1
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.

What am I doing wrong? Can I do that in background?

THANKS

rmaddy
  • 314,917
  • 42
  • 532
  • 579
virusss8
  • 235
  • 1
  • 4
  • 19
  • 1
    You shouldn't do UI work in background thread, it should always be done inside the `.main` thread. – Kamran Jun 18 '19 at 12:39

2 Answers2

1

Don't do any UI actions in a background thread. All UI changes must be done in the main thread

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

Amal T S
  • 3,327
  • 2
  • 24
  • 57
  • 1
    If the `draw` method is for `UIImage`, that's not a view method and should work off the main thread, right? See https://stackoverflow.com/questions/10645307/thread-safety-of-uiimage https://developer.apple.com/forums/thread/29031 https://ahmedk92.github.io/2019/03/23/Rendering-off-main-thread-in-iOS.html – Chris Prince Jan 10 '21 at 20:40
0

It looks like bug in CoreUI working with vector images. 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: enter image description here I was not able to find related retain so I guess because of that memory is released twice which eventually leads to a crash. 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. Maybe we should file a bug report to Apple.

ipos
  • 51
  • 7
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/33253379) – The Dreams Wind Nov 28 '22 at 23:05