swift – Core GraphicsQuartz 2D图像调整大小崩溃

swift – Core GraphicsQuartz 2D图像调整大小崩溃,第1张

概述任务是调整图像大小. 我已阅读this post并采用了CGBitmapContextCreate& CGContextDrawImage方法.这就是我的调整大小功能的样子: extension UIImage { func with(maxHeight: CGFloat, maxWidth: CGFloat) -> UIImage? { guard let image 任务是调整图像大小.

我已阅读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,size: size))

堆栈跟踪:

#11. Crashed: com.apple.root.utility-qos0  CoreGraphics                   0x184d9a59c ERROR_CGDataProvIDer_BufferIsNotReadable + 121  CoreGraphics                   0x184d9a2c0 CGDataProvIDerRetainBytePtr + 2162  CoreGraphics                   0x184e8d06c get_image_pointer + 643  CoreGraphics                   0x184e8d2c8 img_raw_access + 524  CoreGraphics                   0x184e88000 img_interpolate_read + 7085  CoreGraphics                   0x184e8c14c img_data_lock + 70486  CoreGraphics                   0x184e8a56c CGSImageDataLock + 1847  CoreGraphics                   0x184caa628 ripc_AcquireRIPImageData + 3088  CoreGraphics                   0x184e9f1b0 ripc_DrawImage + 6449  CoreGraphics                   0x184e8efac CGContextDrawImageWithOptions + 63210 libswiftCoreGraphics.dylib     0x104781638 (Missing)

所以,我有两个问题:

>我应该怎样做才能重现崩溃情况并解决问题?
>有没有其他方法来获得高质量的重新调整大小的图像?

解决方法 我使用这个更简单的扩展来包装UIImage的绘图(在:)中.我发现它质量很高,速度也相当快.

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的情况.

总结

以上是内存溢出为你收集整理的swift – Core Graphics / Quartz 2D图像调整大小崩溃全部内容,希望文章能够帮你解决swift – Core Graphics / Quartz 2D图像调整大小崩溃所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1016460.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存