The simplest way to resize an UIImage?

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

    Rogerio Chaves answer as a swift extension

    func scaledTo(size: CGSize) -> UIImage{
        UIGraphicsBeginImageContextWithOptions(size, false, 0.0);
        self.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return newImage
    }
    

    And also bonus

    func scaledTo(height: CGFloat) -> UIImage{
        let width = height*self.size.width/self.size.height
        return scaledTo(size: CGSize(width: width, height: height))
    }
    
    0 讨论(0)
  • 2020-11-21 22:48

    Swift 2.0 :

    let image = UIImage(named: "imageName")
    let newSize = CGSize(width: 10, height: 10)
    
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
    image?.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))
    let imageResized = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    
    0 讨论(0)
  • 2020-11-21 22:50

    Swift 4 answer:

    func scaleDown(image: UIImage, withSize: CGSize) -> UIImage {
        let scale = UIScreen.main.scale
        UIGraphicsBeginImageContextWithOptions(withSize, false, scale)
        image.draw(in: CGRect(x: 0, y: 0, width: withSize.width, height: withSize.height))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage!
    }
    
    0 讨论(0)
  • Trevor Howard has some UIImage categories that handle resize quite nicely. If nothing else you can use the code as examples.

    Note: As of iOS 5.1, this answer maybe invalid. See comment below.

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

    Use this extension, in case you need to resize width/height only with aspect ratio.

    extension UIImage {
        func resize(to size: CGSize) -> UIImage {
            return UIGraphicsImageRenderer(size: size).image { _ in
                draw(in: CGRect(origin: .zero, size: size))
            }
        }
        func resize(width: CGFloat) -> UIImage {
            return resize(to: CGSize(width: width, height: width / (size.width / size.height)))
        }
        func resize(height: CGFloat) -> UIImage {
            return resize(to: CGSize(width: height * (size.width / size.height), height: height))
        }
    }
    
    0 讨论(0)
  • 2020-11-21 22:52

    I've discovered that it's difficult to find an answer that you can use out-of-the box in your Swift 3 project. The main problem of other answers that they don't honor the alpha-channel of the image. Here is the technique that I'm using in my projects.

    extension UIImage {
    
        func scaledToFit(toSize newSize: CGSize) -> UIImage {
            if (size.width < newSize.width && size.height < newSize.height) {
                return copy() as! UIImage
            }
    
            let widthScale = newSize.width / size.width
            let heightScale = newSize.height / size.height
    
            let scaleFactor = widthScale < heightScale ? widthScale : heightScale
            let scaledSize = CGSize(width: size.width * scaleFactor, height: size.height * scaleFactor)
    
            return self.scaled(toSize: scaledSize, in: CGRect(x: 0.0, y: 0.0, width: scaledSize.width, height: scaledSize.height))
        }
    
        func scaled(toSize newSize: CGSize, in rect: CGRect) -> UIImage {
            if UIScreen.main.scale == 2.0 {
                UIGraphicsBeginImageContextWithOptions(newSize, !hasAlphaChannel, 2.0)
            }
            else {
                UIGraphicsBeginImageContext(newSize)
            }
    
            draw(in: rect)
            let newImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
    
            return newImage ?? UIImage()
        }
    
        var hasAlphaChannel: Bool {
            guard let alpha = cgImage?.alphaInfo else {
                return false
            }
            return alpha == CGImageAlphaInfo.first ||
                alpha == CGImageAlphaInfo.last ||
                alpha == CGImageAlphaInfo.premultipliedFirst ||
                alpha == CGImageAlphaInfo.premultipliedLast
        }
    }
    

    Example of usage:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let size = CGSize(width: 14.0, height: 14.0)
        if let image = UIImage(named: "barbell")?.scaledToFit(toSize: size) {
            let imageView = UIImageView(image: image)
            imageView.center = CGPoint(x: 100, y: 100)
            view.addSubview(imageView)
        }
    }
    

    This code is a rewrite of Apple's extension with added support for images with and without alpha channel.

    As a further reading I recommend checking this article for different image resizing techniques. Current approach offers decent performance, it operates high-level APIs and easy to understand. I recommend sticking to it unless you find that image resizing is a bottleneck in your performance.

    0 讨论(0)
提交回复
热议问题