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

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

    I ended up using Brads technique to create a scaleToFitWidth method in UIImage+Extensions if that's useful to anyone...

    -(UIImage *)scaleToFitWidth:(CGFloat)width
    {
        CGFloat ratio = width / self.size.width;
        CGFloat height = self.size.height * ratio;
    
        NSLog(@"W:%f H:%f",width,height);
    
        UIGraphicsBeginImageContext(CGSizeMake(width, height));
        [self drawInRect:CGRectMake(0.0f,0.0f,width,height)];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return newImage;
    }
    

    then wherever you like

    #import "UIImage+Extensions.h"

    UIImage *newImage = [image scaleToFitWidth:100.0f];

    Also worth noting you could move this further down into a UIView+Extensions class if you want to render images from a UIView

提交回复
热议问题