1

We have a cross platform image processing library and there is a call that returns a CGImageRef as an UnsafeRawPointer which I need to turn into a CGImage. I know the call works as when using it in objective c I can just cast using (CGImageRef) returnedData on the returned data and I get the image. However when trying to use it in our iOS app using Swift 4 I can't seem to cast it without getting an EXC_BREAKPOINT error or a BAD_ACCESS error. I've tried casting using as! CGImage as well as trying pointer.load(as: CGImage.self) and both give me the same result. Am I doing something wrong? is there a better way to pass back an image from C code to swift? I should mention that the same C function takes a CGImageRef as a parameter and passing a CGImage in instead causes no issues whatsoever.

Julian Hunt
  • 119
  • 3
  • 11
  • Have you checked this question / answers? It's from Swift 3, but possibly the same syntax (or at least might get you there): https://stackoverflow.com/questions/39547504/convert-unsafemutablerawpointer-to-unsafemutablepointert-in-swift-3 – DonMag Oct 12 '18 at 18:56

1 Answers1

3

I think it’s a memory management issue. E.g. the CGImageRef that you get is released before you use it. Use “Product" > “Scheme" > "Edit Scheme" menu, select “Run" section, “Diagnostics” tab and enable “Zombie Objects” option. Then run the app. If some object is used after deallocation, you will get a message in the console.

If that’s the case, something like this should fix the issue:

// pointer is your UnsafeRawPointer for CGImageRef
let cgImage = Unmanaged<CGImage>.fromOpaque(pointer).takeRetainedValue()

Better yet, if you can edit or override header files for the library in question, you can annotate C functions to let Swift know how to handle the pointers properly.

pointum
  • 2,987
  • 24
  • 31
  • This works and pulls the cgImage into that variable however I know get the EXC_BAD_ACCESS somewhere else in the code. What exactly is that line of code doing? It's been a long time since I've had to think about pointers – Julian Hunt Oct 12 '18 at 19:21
  • Changed it to takeUnretainedValue and fixed the memory issue. The c library was freeing the memory so when Swift tried to free cgImage it was already gone. – Julian Hunt Oct 12 '18 at 20:16