0

我有一个 UIImage,我想通过设置 RGBA 来更改一些像素颜色。对于我需要更改的所有像素,我都有 CGPoints。如何通过设置 RGBA 值来更改颜色?

如果无法更改 UIImage 像素的颜色,则可以在包含图像的 UIImageView 上进行绘制,但我需要它表现得非常好,因为我将多次这样做。

我怎样才能做到这一点?

编辑:

我想出了一个解决我的问题的方法。这会将 CGPoint 着色为红色:

-(void)pixelate:(CGPoint)point
{
    CGImageRef inImage = self.drawImage.image.CGImage;
    CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
    if (cgctx == nil) { NSAssert(NO,@"Context shouldn't be nil!"); }

    size_t w = CGImageGetWidth(inImage);
    size_t h = CGImageGetHeight(inImage);
    CGRect rect = {{0,0},{w,h}}; 

    CGContextDrawImage(cgctx, rect, inImage); 

    unsigned char* data = CGBitmapContextGetData(cgctx);
    if (data != nil)
    {
        int offset = 4*((w*round(point.y))+round(point.x));
        data[offset] = 255; //alpha
        data[offset+1] = 255; //red
        data[offset+2] = 0; //green
        data[offset+3] = 0; //blue
    }

    CGImageRef img = CGBitmapContextCreateImage(cgctx);
    UIGraphicsEndImageContext();

    CGContextRelease(cgctx);

    if (data) { free(data); }

    self.drawImage.image = [UIImage imageWithCGImage:img];
}
4

2 回答 2

0

首先导入以下文件.. >>>> 已编辑....

#import <QuartzCore/QuartzCore.h> 

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmContext = CGBitmapContextCreate(NULL, width, height, 8,bytesPerRow,             colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(bitmContext, (CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width =     originalWidth, .size.height = originalHeight}, cgImage);

获取对底层像素的引用:

UInt8* data = (UInt8*)CGBitmapContextGetData(bitmContext);

像素操作:

const size_t bitmapByteCount = bytesPerRow * originalHeight;
for (size_t i = 0; i < bitmapByteCount; i += 4)
{
UInt8 a = data[i];
UInt8 r = data[i + 1];
UInt8 g = data[i + 2];
UInt8 b = data[i + 3];

data[i] = (UInt8)newAlpha
data[i + 1] = (UInt8)newRed;
data[i + 2] = (UInt8)newGreen;
data[i + 3] = (UInt8)newBlue;
}

CGImageRef newImage = CGBitmapContextCreateImage(bitmContext);

或者使用这个参考...

http://www.markj.net/iphone-uiimage-pixel-color/

希望,这会帮助你..享受

于 2012-04-23T06:51:34.060 回答
0

我想出了一个解决我的问题的方法。这会将 CGPoint 着色为红色:

-(void)pixelate:(CGPoint)point
{
    CGImageRef inImage = self.drawImage.image.CGImage;
    CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
    if (cgctx == nil) { NSAssert(NO,@"Context shouldn't be nil!"); }

    size_t w = CGImageGetWidth(inImage);
    size_t h = CGImageGetHeight(inImage);
    CGRect rect = {{0,0},{w,h}}; 

    CGContextDrawImage(cgctx, rect, inImage); 

    unsigned char* data = CGBitmapContextGetData(cgctx);
    if (data != nil)
    {
        int offset = 4*((w*round(point.y))+round(point.x));
        data[offset] = 255; //alpha
        data[offset+1] = 255; //red
        data[offset+2] = 0; //green
        data[offset+3] = 0; //blue
    }

    CGImageRef img = CGBitmapContextCreateImage(cgctx);
    UIGraphicsEndImageContext();

    CGContextRelease(cgctx);

    if (data) { free(data); }

    self.drawImage.image = [UIImage imageWithCGImage:img];
}
于 2012-04-26T07:08:57.447 回答