IPHONE: How to extract UIIMage RGB channels?

I have a UIImage with an alpha channel.

How to extract UIImage RGB channels, each of which will be independent UIImage with alpha?

early.

+3
source share
1 answer

Like this .

Also consider this question - the third answer in the paranoid detail
And some code to access the pixels and save them in a new UIImage:

UIImage* image = ...; // An image
NSData* pixelData = (NSData*) CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
void* pixelBytes = [pixelData bytes];

//Leaves only the green pixel, assuming 32-bit RGBA
for(int i = 0; i < [pixelData length]; i += 4) {
        bytes[i] = 0; // red
        bytes[i+1] = bytes[i+1]; // green
        bytes[i+2] = 0; // blue
        bytes[i+3] = 0; // alpha
    }

NSData* newPixelData = [NSData dataWithBytes:pixelBytes length:[pixelData length]];
UIImage* newImage = [UIImage imageWithData:newPixelData];

adapted from here . To have three different channels in separate images, do as in the code, set everything to zero except the channel that you want to save each time, and then create a new image.

+3

Source: https://habr.com/ru/post/1723952/


All Articles