gpt4 book ai didi

ios - 更改 UIImage 中某些特定像素的颜色

转载 作者:行者123 更新时间:2023-11-29 12:32:21 24 4
gpt4 key购买 nike

我有一个带有一些人像的简单 UIImageView。现在我想根据它们的位置或一些帧值改变一些像素的颜色。如何做到这一点?

任何帮助...

最佳答案

对于长期实现,您应该查看 Core Image Framework tutorial .对于一次性案例,您可以引用 iPhone : How to change color of particular pixel of a UIImage? 中已有的答案。我找到了很好的非 ARC 解决方案,可以在整个帧内更改图片颜色,但您可以尝试采用它仅应用于特定像素:

- (void) grayscale:(UIImage*) image {
CGContextRef ctx;
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);

// Now your rawData contains the image data in the RGBA8888 pixel format.
int byteIndex = (bytesPerRow * 0) + 0 * bytesPerPixel;
for (int ii = 0 ; ii < width * height ; ++ii)
{
// Get color values to construct a UIColor
CGFloat red = (rawData[byteIndex] * 1.0) / 255.0;
CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0;
CGFloat blue = (rawData[byteIndex + 2] * 1.0) / 255.0;
CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;

rawData[byteIndex] = (char) (red);
rawData[byteIndex+1] = (char) (green);
rawData[byteIndex+2] = (char) (blue);

byteIndex += 4;
}

ctx = CGBitmapContextCreate(rawData,
CGImageGetWidth( imageRef ),
CGImageGetHeight( imageRef ),
8,
CGImageGetBytesPerRow( imageRef ),
CGImageGetColorSpace( imageRef ),
kCGImageAlphaPremultipliedLast );

imageRef = CGBitmapContextCreateImage (ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];

CGContextRelease(ctx);

self.workingImage = rawImage;
[self.imageView setImage:self.workingImage];

free(rawData);

}

来源:http://brandontreb.com/image-manipulation-retrieving-and-updating-pixel-values-for-a-uiimage

关于ios - 更改 UIImage 中某些特定像素的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27286767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com