How to easily resize/optimize an image size with iOS?

后端 未结 18 1504
温柔的废话
温柔的废话 2020-11-22 11:05

My application is downloading a set of image files from the network, and saving them to the local iPhone disk. Some of those images are pretty big in size (widths larger tha

18条回答
  •  被撕碎了的回忆
    2020-11-22 11:30

    A problem that might occur on retina displays is that the scale of the image is set by ImageCapture or so. The resize functions above will not change that. In these cases the resize will work not properly.

    In the code below, the scale is set to 1 (not scaled) and the returned image has the size that you would expect. This is done in the UIGraphicsBeginImageContextWithOptions call.

    -(UIImage *)resizeImage :(UIImage *)theImage :(CGSize)theNewSize {
        UIGraphicsBeginImageContextWithOptions(theNewSize, NO, 1.0);
        [theImage drawInRect:CGRectMake(0, 0, theNewSize.width, theNewSize.height)];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }
    

提交回复
热议问题