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

后端 未结 18 1465
温柔的废话
温柔的废话 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:41

    you can use this code to scale image in required size.

    + (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)newSize
    {
        CGSize actSize = image.size;
        float scale = actSize.width/actSize.height;
    
        if (scale < 1) {
            newSize.height = newSize.width/scale;
        } 
        else {
            newSize.width = newSize.height*scale;
        }
    
        UIGraphicsBeginImageContext(newSize);
        [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return newImage;
    }
    

提交回复
热议问题