[英]Scaling image in background thread crashes - UIGraphicsImageRenderer
private func scale(image ciImage: CIImage?, for scale: CGFloat) -> UIImage {
guard let ciImage = ciImage else {
return UIImage()
}
let image = UIImage(ciImage: ciImage)
let size = CGSize(width: image.size.width * scale, height: image.size.height * scale)
let renderer = UIGraphicsImageRenderer(size: size)
return renderer.image(actions: { (context) in
image.draw(in: CGRect(origin: .zero, size: size))
})
}
I am using this method to scale (and generate before that) images.我正在使用这种方法来缩放(并在此之前生成)图像。 This is done in background thread and the result is then dispatched back to main thread.
这是在后台线程中完成的,然后将结果分派回主线程。 However, on image.draw - sometimes 1 out of 10 times, the app crashes with EXC_BAD_ACCESS.
但是,在 image.draw 上——有时十分之一,应用程序会因 EXC_BAD_ACCESS 而崩溃。
What am I doing wrong?我究竟做错了什么? Can I do that in background?
我可以在后台这样做吗?
THANKS谢谢
Don't do any UI actions in a background thread. 不要在后台线程中执行任何UI操作。 All UI changes must be done in the main thread
所有UI更改都必须在主线程中完成
Use a dispatch Queue to work in main thread.Try it like this 使用调度队列在主线程中工作。
DispatchQueue.main.async {
image.draw(in: CGRect(origin: .zero, size: size))
}
Check this answer to know why we use main thread to perform UI actions 检查此答案以了解为什么我们使用主线程执行UI操作
It looks like bug in CoreUI working with vector images.它看起来像 CoreUI 中处理矢量图像的错误。 I'm having a similar issue right now with a following code
我现在遇到类似的问题,代码如下
let scaledImage = renderer.image { _ in
self.draw(in: CGRect(origin: .zero, size: scaledImageSize))
}
leads to the following error:导致以下错误:
Looking into [_CUIThemeSVGRendition rawData]
you can see the following release
statement:查看
[_CUIThemeSVGRendition rawData]
,您可以看到以下release
声明: I was not able to find related
retain
so I guess because of that memory is released twice which eventually leads to a crash.我找不到相关的
retain
,所以我猜是因为 memory 被释放了两次,最终导致崩溃。 It looks like if(r8 & 0x3c) == 0x10)
is checking bitmapEncoding
(see https://developer.limneos.net/index.php?ios=15.2.1&framework=CoreUI.framework&header=CUIThemeRendition.h ) but I have no idea what should be inside.看起来
if(r8 & 0x3c) == 0x10)
正在检查位图编码(参见bitmapEncoding
://developer.limneos.net/index.php?ios=15.2.1&framework=CoreUI.framework&header=CUIThemeRendition.h )但我没有想法里面应该有什么。 Maybe we should file a bug report to Apple.也许我们应该向 Apple 提交错误报告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.