Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Image back from CGBitmapContextGetData

So I have a CGBitmapContext and I get the pixel at a touchlocation using this

unsigned char* data = CGBitmapContextGetData (offscreenBuffer);

if (data != NULL) {
    offset = 4*((self.bounds.size.width*round(coord.y))+round(coord.x));
    int alpha =  data[offset];
    int red = data[offset+1];
    int green = data[offset+2];
    int blue = data[offset+3];

} 

And then I change the pixel colour like below.

data[offset]=255;
data[offset+1]=255;
data[offset+2]=0;
data[offset+3]=0;

My question is how do a get a CGImage back from the modified pixels so that I can update my CGBitmapContext?

Any help will be appreciated, Thanks!

like image 998
EagerMike Avatar asked May 02 '26 23:05

EagerMike


1 Answers

Use CGBitmapContextCreateImage() to generate a new CGImage from your context after modifying the data.


Changes you make to the data returned by CGBitmapContextGetData() should modify the context itself. That's a writable pointer, not a copy.

like image 151
Rob Napier Avatar answered May 05 '26 13:05

Rob Napier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!