The simplest way to resize an UIImage?

前端 未结 30 2595
迷失自我
迷失自我 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 22:43

    Here's a modification of the category written by iWasRobbed above. It keeps the aspect ratio of the original image instead of distorting it.

    - (UIImage*)scaleToSizeKeepAspect:(CGSize)size {
        UIGraphicsBeginImageContext(size);
    
        CGFloat ws = size.width/self.size.width;
        CGFloat hs = size.height/self.size.height;
    
        if (ws > hs) {
            ws = hs/ws;
            hs = 1.0;
        } else {
            hs = ws/hs;
            ws = 1.0;
        }
    
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(context, 0.0, size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
    
        CGContextDrawImage(context, CGRectMake(size.width/2-(size.width*ws)/2,
            size.height/2-(size.height*hs)/2, size.width*ws,
            size.height*hs), self.CGImage);
    
        UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        return scaledImage;
    }
    
    0 讨论(0)
  • 2020-11-21 22:43

    If you want to make a thumbnail of a UIImage (with proportional resizing or maybe some cropping involved), check out UIImage+Resize category that allows you to use concise, ImageMagick-like syntax:

    UIImage* squareImage       = [image resizedImageByMagick: @"320x320#"];
    
    0 讨论(0)
  • 2020-11-21 22:46

    Swift 3.0 with failsafe option (returns the original image in case of error):

    func resize(image: UIImage, toSize size: CGSize) -> UIImage{
        UIGraphicsBeginImageContextWithOptions(size,false,1.0)
        image.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        if let resizedImage = UIGraphicsGetImageFromCurrentImageContext() {
            UIGraphicsEndImageContext()
            return resizedImage
        }
        UIGraphicsEndImageContext()
        return image
    }
    
    0 讨论(0)
  • 2020-11-21 22:47

    This improvement to Paul's code will give you a sharp high res image on an iPhone with a retina display. Otherwise when scaling down it's blurry.

    + (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        if ([[UIScreen mainScreen] scale] == 2.0) {
            UIGraphicsBeginImageContextWithOptions(newSize, YES, 2.0);
        } else {
            UIGraphicsBeginImageContext(newSize);
        }
    } else {
        UIGraphicsBeginImageContext(newSize);
    }
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
    }
    
    0 讨论(0)
  • 2020-11-21 22:47

    I found a category for UIImage in Apple's own examples which does the same trick. Here's the link: https://developer.apple.com/library/ios/samplecode/sc2273/Listings/AirDropSample_UIImage_Resize_m.html.

    You'll just have to change the call:

    UIGraphicsBeginImageContextWithOptions(newSize, YES, 2.0);
    

    in imageWithImage:scaledToSize:inRect: with:

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 2.0);
    

    In order to consider the alpha channel in the image.

    0 讨论(0)
  • 2020-11-21 22:48

    For Swift 5:

    extension UIImage {
      func resized(to newSize: CGSize) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
        defer { UIGraphicsEndImageContext() }
    
        draw(in: CGRect(origin: .zero, size: newSize))
        return UIGraphicsGetImageFromCurrentImageContext()
      }
    }
    
    0 讨论(0)
提交回复
热议问题