Come faccio a scorrere su RGBA di ogni pixel in un contesto bitmap?
-
20-09-2019 - |
Domanda
Come faccio a iterare su ogni pixel in un contesto bitmap? Ho trovato una fonte che sembra essere il tentativo di fare questo, ma è in qualche modo imperfetto. Qualcuno può dirmi come risolvere questo codice in modo che io possa scorrere sulla RGBA di ogni pixel?
-(UIImage*)modifyPixels:(UIImage*)originalImage
{
NSData* pixelData = (NSData*)CGDataProviderCopyData(CGImageGetDataProvider(originalImage.CGImage));
void* pixelBytes = [pixelData bytes];
// Take away the red 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] = bytes[i+2]; // blue
bytes[i+3] = bytes[i+3]; // alpha
}
NSData* newPixelData = [NSData dataWithBytes:pixelBytes length:[pixelData length]];
UIImage* newImage = [UIImage imageWithData:newPixelData];
return newImage;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow