核心图形/Quartz 2D图像调整大小崩溃问题

5

任务是调整图像大小。

我已经阅读了this post并采用了CGBitmapContextCreate和CGContextDrawImage方法。以下是我的调整大小函数:

extension UIImage {

    func with(maxHeight: CGFloat, maxWidth: CGFloat) -> UIImage? {
        guard let image = self.cgImage else {
            return nil
        }
        var height = CGFloat(image.height)
        var width = CGFloat(image.width)
        guard height > 0 && width > 0 else {
            return nil
        }
        let maxHeight = round(maxHeight)
        let maxWidth = round(maxWidth)
        guard height > maxHeight || width > maxWidth else {
            return nil
        }
        let heightProportions = height / maxHeight
        let widthProportions = width / maxWidth
        height = heightProportions > widthProportions ? maxHeight : round(height / widthProportions)
        width = widthProportions > heightProportions ? maxWidth : round(width / heightProportions)
        let size = CGSize(width: width, height: height)
        let bitmapInfo = image.bitmapInfo.rawValue
        let bitsPerComponent = image.bitsPerComponent
        let bytesPerRow = image.bytesPerRow
        let space = image.colorSpace ?? CGColorSpaceCreateDeviceRGB()
        let context = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: space, bitmapInfo: bitmapInfo)
        context?.interpolationQuality = .high
        context?.draw(image, in: CGRect.init(origin: .zero, size: size))
        guard let newImage = context?.makeImage() else {
            return nil
        }
        return UIImage(cgImage: newImage)
    }
}

这个函数在测试期间运行良好,但是在生产环境中出现了崩溃,错误发生在以下代码行:

context?.draw(image, in: CGRect.init(origin: .zero, size: size))

堆栈跟踪:

#11. Crashed: com.apple.root.utility-qos
0  CoreGraphics                   0x184d9a59c ERROR_CGDataProvider_BufferIsNotReadable + 12
1  CoreGraphics                   0x184d9a2c0 CGDataProviderRetainBytePtr + 216
2  CoreGraphics                   0x184e8d06c get_image_pointer + 64
3  CoreGraphics                   0x184e8d2c8 img_raw_access + 52
4  CoreGraphics                   0x184e88000 img_interpolate_read + 708
5  CoreGraphics                   0x184e8c14c img_data_lock + 7048
6  CoreGraphics                   0x184e8a56c CGSImageDataLock + 184
7  CoreGraphics                   0x184caa628 ripc_AcquireRIPImageData + 308
8  CoreGraphics                   0x184e9f1b0 ripc_DrawImage + 644
9  CoreGraphics                   0x184e8efac CGContextDrawImageWithOptions + 632
10 libswiftCoreGraphics.dylib     0x104781638 (Missing)

所以,我有两个问题:

  1. 我应该做什么来重现崩溃的情况并修复问题?
  2. 是否有其他方法获得质量良好的调整大小的图像?
1个回答

1

我使用这个简单的扩展来包装UIImage的draw(in:)。我发现它质量高且速度合理。

extension UIImage {

    public func resized(maxSize: CGSize) -> UIImage? {
        let imageSize = self.size
        guard imageSize.height > 0, imageSize.width > 0 else { return nil }

        let ratio = min(maxSize.width/imageSize.width, maxSize.height/imageSize.height)
        let newSize = CGSize(width: imageSize.width*ratio, height: imageSize.height*ratio)

        let renderer = UIGraphicsImageRenderer(size: newSize)
        return renderer.image(actions: { (ctx) in
            self.draw(in: CGRect(origin: .zero, size: newSize))
        })
    }
}

使用方法:

let resizedImage = myImage.resized(maxSize: CGSize(width: maxWidth, height: maxHeight))

请注意,这里的大小是以点为单位测量的,这意味着底层像素的数量将自动乘以设备的比例因子(例如,视网膜显示器为2倍)。换句话说,此方法适用于将生成的UIImage显示在屏幕上的情况。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接