ios - 如何在 Swift 中调整图像大小?

标签 ios swift uiimage

我正在使用 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 中创建所选图片的 200x200 像素版本(然后上传)?

还有,thumbnailImage 函数在做什么?

最佳答案

查看我的博文,Resize image in swift and objective C ,了解更多详情。

swift 中的图像调整大小功能如下。

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(origin: .zero, size: newSize)
    
    // 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
}

使用上面的函数并将图像调整为 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!
}

关于ios - 如何在 Swift 中调整图像大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31314412/

相关文章:

ios - UIImage(contentsOfFile :) returning nil despite file existing in caches directory

ios - App Store 的应用内购买可以退款吗?

ios - "Scollable content size ambiguity"尽管硬编码宽度和高度?

ios - 在选择一行之前,detailTextLabel 不会显示

ios - 在透明导航栏之间过渡到半透明

ios - 快速从日期选择器获取日期

ios - 带有图像的自定义后退按钮

macos - NSWindow 全尺寸内容 View 和启动弹出窗口导致错误

ios - UIImage imageNamed 返回 nil

ios - 检测在 uiscrollView 中点击了哪个 UIImage