IOS中图片的一些处理方法

1. 图片缩放到指定大小, 但这个会造成图片的清晰度下降

- (UIImage *)imageWithSize:(CGSize)targetSize {

    UIImage *sourceImage = self;

    UIImage *newImage = nil;

    CGFloat targetWidth = targetSize.width;

    CGFloat targetHeight = targetSize.height;

    CGFloat scaledWidth = targetWidth;

    CGFloat scaledHeight = targetHeight;

    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

    UIGraphicsBeginImageContext(targetSize);

    CGRect thumbnailRect = CGRectZero;

    thumbnailRect.origin = thumbnailPoint;

    thumbnailRect.size.width  = scaledWidth;

    thumbnailRect.size.height = scaledHeight;

    [sourceImage drawInRect:thumbnailRect];

    newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;

}
缩放的代码

IOS中图片的一些处理方法 IOS中图片的一些处理方法

左图是缩放后的,右图是原图

左边比较模糊。不建议使用这种图片缩放方法,即便是1:1的变化也会造成图片模糊。

 

你可能感兴趣的:(ios)