Objective c 编辑UIImage中的颜色字节

Objective c 编辑UIImage中的颜色字节,objective-c,ios,uiimage,bytearray,rgba,Objective C,Ios,Uiimage,Bytearray,Rgba,我对字节级的UIImages很陌生,但我希望有人能给我提供一些关于这个问题的指南 我最终希望根据某些参数(位置、颜色等)编辑字节的RGBA值,我知道我以前遇到过这方面的示例/教程,但我现在似乎找不到任何东西 基本上,我希望能够将UIImage分解为字节,并对它们进行迭代,然后分别编辑字节的RGBA值。也许这里的一些示例代码也会有很大的帮助 我已经在不同的图像环境中工作,并使用CG power工具编辑图像,但我希望能够在字节级别工作 编辑: 抱歉,但我明白您不能直接编辑UIImage中的字节。我应

我对字节级的UIImages很陌生,但我希望有人能给我提供一些关于这个问题的指南

我最终希望根据某些参数(位置、颜色等)编辑字节的RGBA值,我知道我以前遇到过这方面的示例/教程,但我现在似乎找不到任何东西

基本上,我希望能够将UIImage分解为字节,并对它们进行迭代,然后分别编辑字节的RGBA值。也许这里的一些示例代码也会有很大的帮助

我已经在不同的图像环境中工作,并使用CG power工具编辑图像,但我希望能够在字节级别工作

编辑:

抱歉,但我明白您不能直接编辑UIImage中的字节。我应该问得更清楚些。我想问的是如何获取UIImage的字节,编辑这些字节,然后从这些字节创建新的UIImage


正如@BradLarson所指出的,OpenGL是一个更好的选择,并且有一个很棒的库,它是由@BradLarson创建的。感谢@CSmith指出这一点

您无法直接访问
UIImage
中的字节,也无法直接更改它们


您必须将图像绘制到
CGBitmapContext
,修改位图中的像素,然后从位图上下文创建新图像。

您无法直接访问
UIImage
中的字节,并且无法直接更改它们


您必须将图像绘制到
CGBitmapContext
,修改位图中的像素,然后从位图上下文创建新图像。

@MartinR的答案是正确的,以下是一些代码可以帮助您开始:

UIImage*image=您的图像

CGImageRef imageRef = image.CGImage;
NSUInteger nWidth = CGImageGetWidth(imageRef);
NSUInteger nHeight = CGImageGetHeight(imageRef);
NSUInteger nBytesPerRow = CGImageGetBytesPerRow(imageRef);
NSUInteger nBitsPerPixel = CGImageGetBitsPerPixel(imageRef);
NSUInteger nBitsPerComponent = CGImageGetBitsPerComponent(imageRef);
NSUInteger nBytesPerPixel = nBitsPerPixel == 24 ? 3 : 4;

unsigned char *rawInput = malloc (nWidth * nHeight * nBytesPerPixel);

CGColorSpaceRef colorSpaceRGB = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(rawInput, nWidth, nHeight, nBitsPerComponent, nBytesPerRow, colorSpaceRGB, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Big);
CGContextDrawImage (context, CGRectMake(0, 0, nWidth, nHeight), imageRef);          

// modify the pixels stored in the array of 4-byte pixels at rawInput
.
.
.

UIImage *imageNew = [[UIImage alloc] initWithCGImage:CGBitmapContextCreateImage(context)];

CGContextRelease (context);
free (rawInput);

@MartinR给出了正确的答案,以下是一些让您开始学习的代码:

UIImage*image=您的图像

CGImageRef imageRef = image.CGImage;
NSUInteger nWidth = CGImageGetWidth(imageRef);
NSUInteger nHeight = CGImageGetHeight(imageRef);
NSUInteger nBytesPerRow = CGImageGetBytesPerRow(imageRef);
NSUInteger nBitsPerPixel = CGImageGetBitsPerPixel(imageRef);
NSUInteger nBitsPerComponent = CGImageGetBitsPerComponent(imageRef);
NSUInteger nBytesPerPixel = nBitsPerPixel == 24 ? 3 : 4;

unsigned char *rawInput = malloc (nWidth * nHeight * nBytesPerPixel);

CGColorSpaceRef colorSpaceRGB = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(rawInput, nWidth, nHeight, nBitsPerComponent, nBytesPerRow, colorSpaceRGB, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Big);
CGContextDrawImage (context, CGRectMake(0, 0, nWidth, nHeight), imageRef);          

// modify the pixels stored in the array of 4-byte pixels at rawInput
.
.
.

UIImage *imageNew = [[UIImage alloc] initWithCGImage:CGBitmapContextCreateImage(context)];

CGContextRelease (context);
free (rawInput);

您具体想对图像字节做什么?在每个像素上运行操作?如果是这样,您可能希望考虑使用OpenGL ES片段着色器,而不是在CPU上对像素进行较慢的迭代。OpenGL ES实现比CPU绑定循环快100倍。@BradLarson有很棒的GPUImage库,你可以下载来学习OpenGL ES编程,为我工作@布拉德拉森,非常感谢!我一定会浏览CSmith指出的GPUImage开源库。谢谢你的帮助!您具体想对图像字节做什么?在每个像素上运行操作?如果是这样,您可能希望考虑使用OpenGL ES片段着色器,而不是在CPU上对像素进行较慢的迭代。OpenGL ES实现比CPU绑定循环快100倍。@BradLarson有很棒的GPUImage库,你可以下载来学习OpenGL ES编程,为我工作@布拉德拉森,非常感谢!我一定会浏览CSmith指出的GPUImage开源库。谢谢你的帮助!要修改特定像素的RGBA内容,我将按如下方式访问它们:R=>rawInput[inBitsPerPixel],G=>rawInput[inBitsPerPixel+NbitPerComponent],B=>rawInput[inBitsPerPixel+NbitPerComponent*2],a=>rawInput[inBitsPerPixel+NbitPerComponent*3],其中我是迭代中的位置?我可以直接设置这些值(我假设它们是从0.0f到1.0f的)吗?每个像素是四个字节,RGBA(例如nBytesPerPixel==4)。它们是字节,不是浮点数。第0行、第0列是前四个字节,第0行、第1列是下一个四个字节,等等。虽然这会让您继续,但OpenGL值得付出努力,尤其是在位图较大的情况下。要设置第一个像素的蓝色,基本上,我可以访问字节数组中的第三个位置,并将其设置为0xff(255 blue)?要修改特定像素的RGBA内容,我可以这样访问它们:R=>rawInput[inBitsPerPixel],G=>rawInput[inBitsPerPixel+NbitPerComponent],B=>rawInput[inBitsPerPixel+NbitPerComponent*2],a=>rawInput[InBitPerPixel+NbitPerComponent*3],其中i是迭代中的位置?我可以直接设置值(我假设它们从0.0f到1.0f)吗?每个像素有四个字节,RGBA(例如NbyteSperpix==4)。它们是字节,不是浮点数。第0行、第0列是前四个字节,第0行、第1列是下四个字节,等等。虽然这会让您继续,但OpenGL值得付出努力,尤其是在位图很大的情况下。要设置第一个像素的蓝色,我基本上会访问字节数组中的第三个位置,我可以将其设置为0xff,表示255蓝色?