如何在Swift中调整图片大小?

我正在使用Swift和Parse.com 为iOS制作一个应用程序


我试图让用户从图像选择器中选择一张图片,然后将所选图像的大小调整为200x200像素,然后再上传到我的后端。


Parse.com有一个名为“ AnyPic”的Instagram复制应用程序的教程,该教程提供了用于调整图像大小的代码,但它位于Objective-C中。


// Resize the image to be square (what is shown in the preview)

UIImage *resizedImage = [anImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit

        bounds:CGSizeMake(560.0f, 560.0f)

        interpolationQuality:kCGInterpolationHigh];

// Create a thumbnail and add a corner radius for use in table views

UIImage *thumbnailImage = [anImage thumbnailImage:86.0f

        transparentBorder:0.0f

        cornerRadius:10.0f

        interpolationQuality:kCGInterpolationDefault];

如何在Swift中为所选图片创建200x200px版本(然后上传)?


而且,thumbnailImage函数在做什么?


宝慕林4294392
浏览 1153回答 3
3回答

慕容708150

有关更多详细信息,请参阅我的博客文章,快速且客观地C调整图像大小。快速调整图像尺寸的功能如下。func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {    let size = image.size    let widthRatio  = targetSize.width  / size.width    let heightRatio = targetSize.height / size.height    // Figure out what our orientation is, and use that to form the rectangle    var newSize: CGSize    if(widthRatio > heightRatio) {        newSize = CGSizeMake(size.width * heightRatio, size.height * heightRatio)    } else {        newSize = CGSizeMake(size.width * widthRatio,  size.height * widthRatio)    }    // This is the rect that we've calculated out and this is what is actually used below    let rect = CGRectMake(0, 0, newSize.width, newSize.height)    // Actually do the resizing to the rect using the ImageContext stuff    UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)    image.drawInRect(rect)    let newImage = UIGraphicsGetImageFromCurrentImageContext()    UIGraphicsEndImageContext()    return newImage}使用上面的功能并使用200 * 200调整图像大小,如下代码self.resizeImage(UIImage(named: "yourImageName")!, targetSize: CGSizeMake(200.0, 200.0))swift3更新 func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {    let size = image.size    let widthRatio  = targetSize.width  / size.width    let heightRatio = targetSize.height / size.height    // Figure out what our orientation is, and use that to form the rectangle    var newSize: CGSize    if(widthRatio > heightRatio) {        newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)    } else {        newSize = CGSize(width: size.width * widthRatio,  height: size.height * widthRatio)    }    // This is the rect that we've calculated out and this is what is actually used below    let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)    // Actually do the resizing to the rect using the ImageContext stuff    UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)    image.draw(in: rect)    let newImage = UIGraphicsGetImageFromCurrentImageContext()    UIGraphicsEndImageContext()    return newImage!}

慕慕森

由于@KiritModi的答案来自2015年,因此这是Swift 3.0的版本:func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {    let size = image.size    let widthRatio  = targetSize.width  / image.size.width    let heightRatio = targetSize.height / image.size.height    // Figure out what our orientation is, and use that to form the rectangle    var newSize: CGSize    if(widthRatio > heightRatio) {        newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)    } else {        newSize = CGSize(width: size.width * widthRatio,  height: size.height * widthRatio)    }    // This is the rect that we've calculated out and this is what is actually used below    let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)    // Actually do the resizing to the rect using the ImageContext stuff    UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)    image.draw(in: rect)    let newImage = UIGraphicsGetImageFromCurrentImageContext()    UIGraphicsEndImageContext()    return newImage!}

MM们

对于Swift 4.0和iOS 10    extension UIImage {        func resizeImage(_ dimension: CGFloat, opaque: Bool, contentMode: UIViewContentMode = .scaleAspectFit) -> UIImage {            var width: CGFloat            var height: CGFloat            var newImage: UIImage            let size = self.size            let aspectRatio =  size.width/size.height            switch contentMode {                case .scaleAspectFit:                    if aspectRatio > 1 {                            // Landscape image                        width = dimension                        height = dimension / aspectRatio                    } else {                                        // Portrait image                        height = dimension                        width = dimension * aspectRatio                    }            default:                fatalError("UIIMage.resizeToFit(): FATAL: Unimplemented ContentMode")            }            if #available(iOS 10.0, *) {                let renderFormat = UIGraphicsImageRendererFormat.default()                renderFormat.opaque = opaque                let renderer = UIGraphicsImageRenderer(size: CGSize(width: width, height: height), format: renderFormat)                newImage = renderer.image {                    (context) in                    self.draw(in: CGRect(x: 0, y: 0, width: width, height: height))                }            } else {                UIGraphicsBeginImageContextWithOptions(CGSize(width: width, height: height), opaque, 0)                    self.draw(in: CGRect(x: 0, y: 0, width: width, height: height))                    newImage = UIGraphicsGetImageFromCurrentImageContext()!                UIGraphicsEndImageContext()            }            return newImage        }    }
打开App,查看更多内容
随时随地看视频慕课网APP