首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIImage质量

UIImage质量
EN

Stack Overflow用户
提问于 2016-04-19 15:08:40
回答 1查看 585关注 0票数 0

我正在用这个功能调整各种图像的大小:

代码语言:javascript
复制
    func resizeImage(image: UIImage) -> UIImage {
        var actualHeight: CGFloat = image.size.height
        var actualWidth: CGFloat = image.size.width

        let maxHeight: CGFloat = 600.0
        let maxWidth: CGFloat = text.frame.size.width - 10

        var imgRatio: CGFloat = actualWidth / actualHeight
        let maxRatio: CGFloat = maxWidth / maxHeight
        let compressionQuality: CGFloat = 0.5
        //50 percent compression
        if actualHeight > maxHeight || actualWidth > maxWidth {
            if imgRatio < maxRatio {
                //adjust width according to maxHeight
                imgRatio = maxHeight / actualHeight
                actualWidth = imgRatio * actualWidth
                actualHeight = maxHeight
            }
            else if imgRatio > maxRatio {
                //adjust height according to maxWidth
                imgRatio = maxWidth / actualWidth
                actualHeight = imgRatio * actualHeight
                actualWidth = maxWidth
            }
            else {
                actualHeight = maxHeight
                actualWidth = maxWidth
            }
        }
        let rect: CGRect = CGRectMake(0.0, 0.0, actualWidth, actualHeight)
        UIGraphicsBeginImageContext(rect.size)
        image.drawInRect(rect)
        let img: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        let imageData: NSData = UIImageJPEGRepresentation(img, compressionQuality)!
        UIGraphicsEndImageContext()

        return UIImage(data: imageData)!
    }

但质量只是terrible....Heres,这张照片反映了我的心声:

我以为图像的质量会因compressionQuality是什么而变化.?正如您从上面的代码中可以看到的那样,我现在的值是0.5,但是即使我将它更改为1,质量仍然很糟糕吗?

任何想法,

非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-19 15:38:21

您应该使用UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)代替。它将对您的设备使用正确的刻度。您使用的版本(没有选项)使用1.0的默认缩放因子。使用...WithOptions()变量并为缩放传递0.0,默认为设备的本机比例尺。请注意,这可能不是您想要的。

其次,可以使用AVMakeRectWithAspectRatioInsideRect()计算目标大小。第三,不需要经过JPEG编码,可以直接返回生成的图像。

综合起来,这大大简化了您的代码:

代码语言:javascript
复制
func resizeImage(image: UIImage) -> UIImage {
  let maxSize = CGSize(
    width: text.frame.size.width - 10, 
    height: 600.0
  )
  let newSize = AVMakeRectWithAspectRatioInsideRect(
    image.size, 
    CGRect(origin: CGPointZero, size: maxSize)
  ).size
  UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
  image.drawInRect(CGRect(origin: CGPointZero, size: newSize))
  let scaled = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext()
  return scaled
}

编辑:这是我在项目中使用的UIImage上的一个扩展:

代码语言:javascript
复制
extension UIImage {
    /**
     Returns an scaled version of self that fits within the provided bounding box.
     It retains aspect ratio, and also retains the rendering mode.
     - parameter size: The target size, in point
     - parameter scale: Optional target scale. If omitted, uses the scale of input image
     - returns: A scaled image
     */
    func imageFittingSize(size: CGSize, scale: CGFloat? = nil) -> UIImage {
        let newSize = AVMakeRectWithAspectRatioInsideRect(self.size, CGRect(origin: CGPointZero, size: size)).size
        UIGraphicsBeginImageContextWithOptions(newSize, false, scale ?? self.scale)
        self.drawInRect(CGRect(origin: CGPointZero, size: newSize))
        let scaled = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return scaled.imageWithRenderingMode(renderingMode)
    }
}

您可以在这样的图像上调用它:

代码语言:javascript
复制
let maxSize = CGSize(width: text.frame.size.width - 10, height: 600.0)
let scaled = image.imageFittingSize(maxSize)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36722402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档