【发布时间】:2009-08-29 09:41:49
【问题描述】:
如何在位图上下文中迭代每个像素?我发现了一些似乎试图这样做的来源,但它存在某种缺陷。谁能告诉我如何修复此代码,以便我可以迭代每个像素的 RGBA?
-(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;
}
【问题讨论】:
标签: iphone uikit uiimage core-graphics cgimage