The simplest way to resize an UIImage?

前端 未结 30 2639
迷失自我
迷失自我 2020-11-21 22:38

In my iPhone app, I take a picture with the camera, then I want to resize it to 290*390 pixels. I was using this method to resize the image :

UIImage *newI         


        
30条回答
  •  难免孤独
    2020-11-21 23:07

    If you just want an image smaller and don't care about exact size:

    + (UIImage *)imageWithImage:(UIImage *)image scaledToScale:(CGFloat)scale
    {
        UIGraphicsBeginImageContextWithOptions(self.size, YES, scale);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
        [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }
    

    Setting scale to 0.25f will give you a 816 by 612 image from a 8MP camera.

    Here's a category UIImage+Scale for those who needs one.

提交回复
热议问题