The simplest way to resize an UIImage?

前端 未结 30 2596
迷失自我
迷失自我 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:03

    For my fellow Xamarians, here is a Xamarin.iOS C# version of @Paul Lynch answer.

    private UIImage ResizeImage(UIImage image, CGSize newSize) 
    {
        UIGraphics.BeginImageContextWithOptions(newSize, false, 0.0f);
        image.Draw(new CGRect(0, 0, newSize.Width, newSize.Height));
        UIImage newImage = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();
        return newImage;
    }
    
    0 讨论(0)
  • 2020-11-21 23:05

    Why so complicated? I think using system API can achieve the same result:

    UIImage *largeImage;
    CGFloat ratio = 0.4; // you want to get a new image that is 40% the size of large image.
    UIImage *newImage = [UIImage imageWithCGImage:largeImage.CGImage
                                            scale:1/ratio
                                      orientation:largeImage.imageOrientation];
    // notice the second argument, it is 1/ratio, not ratio.
    

    The only gotcha is you should pass inverse of target ratio as the second argument, as according to the document the second parameter specifies the ratio of original image compared to the new scaled one.

    0 讨论(0)
  • 2020-11-21 23:06

    The simplest way is to set the frame of your UIImageView and set the contentMode to one of the resizing options.

    Or you can use this utility method, if you actually need to resize an image:

    + (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
        //UIGraphicsBeginImageContext(newSize);
        // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
        // Pass 1.0 to force exact pixel size.
        UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
        [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
        UIGraphicsEndImageContext();
        return newImage;
    }
    

    Example usage:

    #import "MYUtil.h"
    …
    UIImage *myIcon = [MYUtil imageWithImage:myUIImageInstance scaledToSize:CGSizeMake(20, 20)];
    
    0 讨论(0)
  • 2020-11-21 23:06

    use this extension

    extension UIImage {
        public func resize(size:CGSize, completionHandler:(resizedImage:UIImage, data:NSData?)->()) {
            dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), { () -> Void in
                let newSize:CGSize = size
                let rect = CGRectMake(0, 0, newSize.width, newSize.height)
                UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
                self.drawInRect(rect)
                let newImage = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
                let imageData = UIImageJPEGRepresentation(newImage, 0.5)
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    completionHandler(resizedImage: newImage, data:imageData)
                })
            })
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 23:07

    Effective approach without stretching image Swift 4

    // Method to resize image
    func resize(image: UIImage, toScaleSize:CGSize) -> UIImage {
                    UIGraphicsBeginImageContextWithOptions(toScaleSize, true, image.scale)
                            image.draw(in: CGRect(x: 0, y: 0, width: toScaleSize.width, height: toScaleSize.height))
                            let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
                            UIGraphicsEndImageContext()
                            return scaledImage!
                    }
    

    // Call method

        let resizedImage = self.resize(image: UIImage(named: "YourImageName")!, toScaleSize: CGSize(width: 290, height: 390))
    
    0 讨论(0)
提交回复
热议问题