When generating a CGImage
one can choose between an initializer using JPEG-encoded data, PNG-encoded data or just data. I'm generating an image from a Data
object containing 16 bit grayscale values.
The initializer required a CGDataProvider
which could be Sequential-Access or Direct-Access Data Provider.
Under the Sequential-Access we have only one initializer:
- Creates a sequential-access data provider
Under the Direct-Access we have multiple initializers but let's focus on a subset:
- Creates a direct-access data provider
- Creates a direct-access data provider that uses data your program supplies
- Creates a data provider that reads from a CFData object
We can definitely notice that the last item in the Direct-Access list missing the "direct-access" term in its definition.
First question: I understand the notion of sequential and direct access to memory, but can someone give me a more precise definition taking into consideration Swift's behaviour?
Let's move on.
I have two different Data
objects in my subclass of UIViewController
.
I'm calling from the main dispatch queue to a static function I have in a helper class.
This helper function gets the Data
and this is where I'm creating the CGImage
and return it to the view controller.
The view controller saves each image in a separate property variable.
This is where things get weird:
Using "Creates a data provider that reads from a CFData object" - works!
But I don't want to use CoreFoundation inside an iOS project and create a CFData
object (which probably copies the buffer into a new one)
Using "Creates a direct-access data provider that uses data your program supplies" - works!
I'm accessing the raw buffer using withUnsafeBytes
which provides me an UnsafeRawBufferPointer
inside a closure.
But if I change the access to the Data
using withUnsafeMutableBytes
- the second image returned to the view controller overrides the first image!
I tried to debug it with XCode and each image is actually generated appropriately inside the helper function, but when the second image returned to the view controller, the first image is changed.
Second question: What could create this weird behaviour?
There are a lot of participants in this process and I think I did everything I could to try and resolve it. Any feedback would be kindly appreciated.