I have an UIImage with alpha channel.
我有一个带有通道的UIImage。
How do I extract the RGB channels of a UIImage, each one to be an independent UIImage with alpha?
如何提取UIImage的RGB通道,每个通道都是独立的UIImage ?
thanks in advance.
提前谢谢。
1 个解决方案
#1
3
Like this.
像这样。
Also, take a look at this question - third answer in paranoid detail
And some code for accessing the pixels and saving them into a new UIImage:
另外,看看这个问题——第三个问题的详细答案和一些用于访问像素并将它们保存到新的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 the three distinct channels in separate images, do like in the code, set to zero all but the channel that you want to save each time, and then create the new image.
改编自这里。要想在不同的映像中拥有三个不同的通道,请像代码中那样,将每次保存的通道设置为0,然后创建新的映像。
#1
3
Like this.
像这样。
Also, take a look at this question - third answer in paranoid detail
And some code for accessing the pixels and saving them into a new UIImage:
另外,看看这个问题——第三个问题的详细答案和一些用于访问像素并将它们保存到新的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 the three distinct channels in separate images, do like in the code, set to zero all but the channel that you want to save each time, and then create the new image.
改编自这里。要想在不同的映像中拥有三个不同的通道,请像代码中那样,将每次保存的通道设置为0,然后创建新的映像。