• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何调整图像大小并避免其模糊?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 21:51:03 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我目前正在使用不模糊的代码,但它只是剪掉了一部分,如果我尝试在线使用不同的代码,它会剪掉一部分并且它也会变得模糊。我需要做什么,图片是 512x512 试图将其缩小到 100x100,它是 jpg。

这是我现在使用的代码:

//setting up each cell
    -(UICollectionViewCell *)collectionViewUICollectionView *)collectionView
                     cellForItemAtIndexPathNSIndexPath *)indexPath
    {
        CollectionGridCell *myCell = [collectionView
                                        dequeueReusableCellWithReuseIdentifier"GridCell"
                                        forIndexPath:indexPath];


        long row = [indexPath row];

           NSURL *baseUrl = [NSURL URLWithString"http://example.com/"];

    NSString *imageItemName = [homeImages objectAtIndex:row];
    NSURL *url = [NSURL URLWithString:imageItemName relativeToURL:baseUrl];

   // NSURL *url = /* prepare a url... see note below */
    [myCell.homeImage setImageWithURL:url
                     placeholderImage:[UIImage imageNamed"menuButton.png"]
                            completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                                // inspect cacheType here to make sure it's cached as you want it
                               myCell.homeImage.image = [self resizeImage:image newSize:CGSizeMake(75,75)];

                            }];

    return myCell;

}


- (UIImage *)resizeImageUIImage*)image newSizeCGSize)newSize {
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
    CGImageRef imageRef = image.CGImage;

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Set the quality level to use when rescaling
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

    CGContextConcatCTM(context, flipVertical);
    // Draw into the context; this scales the image
    CGContextDrawImage(context, newRect, imageRef);

    // Get the resized image from the context and a UIImage
    CGImageRef newImageRef = CGBitmapContextCreateImage(context);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

    CGImageRelease(newImageRef);
    UIGraphicsEndImageContext();

    return newImage;

}

更新:到目前为止,我尝试过的每一种方法,甚至是下面的方法,都会导致中间出现裁剪。我是否必须在我的代码中指定其他内容,是否有什么东西调用它来裁剪中间?

这是我的网格单元 .h 文件:

#import <UIKit/UIKit.h>

@interface CollectionGridCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *homeImage;

@end

这是我的网格单元 .m 文件:

#import "CollectionGridCell.h"

@implementation CollectionGridCell

- (id)initWithFrameCGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRectCGRect)rect
{
    // Drawing code
}
*/

@end



Best Answer-推荐答案


无需重新调整图像大小。只需将以下属性设置为您的 ImageView 。

 [myCell.homeImage setImageWithURL:url
                     placeholderImage:[UIImage imageNamed"menuButton.png"]
                            completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                                // inspect cacheType here to make sure it's cached as you want it
                               myCell.homeImage.image = image;
                               myCell.homeImage.contentMode = UIViewContentModeScaleAspectFit;
                            }];

关于ios - 如何调整图像大小并避免其模糊?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23333498/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap