OGeek|极客世界-中国程序员成长平台

标题: iphone - 是否可以在不损失质量的情况下调整 UIImage 的大小? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:30
标题: iphone - 是否可以在不损失质量的情况下调整 UIImage 的大小?

我有一个 UIImage,我把它放在一个 UIImageView 中,它附有一个捏手势识别器,我用它来缩放图像。当我在 UIImageView 中缩放图像时,没有失真并且可以完美缩放。但是,我需要在不在 UIImageView 中时调整 UIImage 的大小,并且当我使用以下代码执行此操作时,结果略有偏差:

CGFloat width = self.imageView.frame.size.width;
CGFloat height = self.imageView.frame.size.height;

UIGraphicsBeginImageContext(CGSizeMake(width, height));
[self.image drawInRect:CGRectMake(0, 0, width, height)];
UIImage *resizedImage = [UIGraphicsGetImageFromCurrentImageContext() retain];    
UIGraphicsEndImageContext();

如您所见,我从 imageView 获取高度和宽度,因为这是图像需要缩放到的。

我知道如果 imageView 能够做到这一点,它必须可以完美地缩放它,有没有人碰巧知道我做错了什么?

更新:我附加了图像的前/后(已由用户在 ImageView 中重新缩放)以及使用上述方法调整大小后的外观。 Before After



Best Answer-推荐答案


试试这个:

+ (UIImage*)rescaleImageUIImage *)image scaledToSizeCGRect)newSize {
    UIGraphicsBeginImageContext(newSize.size);
    [image drawInRect:newSize];
    UIImage *resImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resImage;
}

关于iphone - 是否可以在不损失质量的情况下调整 UIImage 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8174315/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4