简体   繁体   English

UIImage的质量

[英]Quality of UIImage

I am resizing various images with this func: 我正在使用此功能调整各种图像的大小:

    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)!
    }

But the quality is just terrible....Heres a picture of what I get bellow: 但是质量真是太糟糕了...。。。下面是我的吼叫:

在此处输入图片说明

I thought the quality of the image varied on what the compressionQuality was...? 我认为图像的质量随压缩质量的变化而变化...? I have it at 0.5 now as you can see from my code above, but even if I change it to 1 the quality is still terrible? 从上面的代码中可以看到,现在的值为0.5,但是即使将其更改为1,质量仍然很差吗?

Any ideas, 有任何想法吗,

Many thanks. 非常感谢。

You should use UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0) instead. 您应该改用UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0) It will use the correct scale for your device. 它将为您的设备使用正确的比例。 The version you're using (without options), uses a default scale factor of 1.0 . 您使用的版本(不带选项)使用默认比例因子1.0 Using the ...WithOptions() variant and passing in 0.0 for the scale, defaults to the native scale of the device. 使用...WithOptions()变体并为比例传递0.0 ,默认为设备的原始比例。 Note that this might not be what you want. 请注意,这可能不是您想要的。

Secondly, you can use AVMakeRectWithAspectRatioInsideRect() to calculate your target size. 其次,您可以使用AVMakeRectWithAspectRatioInsideRect()计算目标大小。 Thirdly, you don't need to go through the JPEG encoding, you can return the generated image directly. 第三,您不需要进行JPEG编码,您可以直接返回生成的图像。

Combined, this greatly simplifies your code: 综合起来,这大大简化了您的代码:

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
}

EDIT: This is an extension on UIImage that I use in a project: 编辑:这是我在项目中使用的UIImage的扩展:

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)
    }
}

You can call it on an image like so: 您可以像这样在图像上调用它:

let maxSize = CGSize(width: text.frame.size.width - 10, height: 600.0)
let scaled = image.imageFittingSize(maxSize)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM