swift - Core Graphics/Quartz 2D 图像大小调整崩溃

标签 swift uiimage core-graphics cgcontext

任务是调整图像的大小。

我已阅读 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. 是否有任何其他方法可以获得质量好的调整大小的图像?

最佳答案

我使用这个更简单的扩展来包装 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))

请注意,此处的大小以点为单位,这意味着基础像素数将自动乘以设备的比例因子(例如,视网膜显示器为 2x)。换句话说,此方法专为生成的 UIImage 将显示在屏幕上的情况而设计。

关于swift - Core Graphics/Quartz 2D 图像大小调整崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49429715/

相关文章:

ios - PDFKit 注释 url 因不可为 null 的属性(可以为 null)而崩溃

swift - 服务器记录中的 CKAsset 不包含 fileURL,甚至无法检查 nil

ios - iOS 11 中的 UITableViewCell 重新排序样式

ios - 如何根据 UIImage 大小调整 UIImageView 的大小并适合屏幕的宽度

ios - 从函数中的数组返回一个项目

ios - 苹果登录自定义按钮

ios - 如何将 UIImage 数组导出为电影?

ios - Swift:使用 drawStroke(_:touch:) 绘制一条半透明线

ios - -[CALayer setNeedsDisplayInRect :] causes the whole layer to be redrawn

ios - 为神经科学项目研究目的构建 'DrawSomething' 风格的应用程序