通过像素阵列更改UIImage的颜色

时间:2013-07-03 04:41:38

标签: objective-c colors uiimage core-graphics pixels

我想改变图像的颜色但不想改变图像的alpha值。 我正在使用以下代码以蓝色更改颜色。

但我想将所有像素阵列的图像更改为相应的RGB值。

就像我必须应用RGB值(R = 116 G = 170 B = 243)。

CGImageRef sourceImage = ImageView_Test.image.CGImage;

        CFDataRef theData;
        theData = CGDataProviderCopyData(CGImageGetDataProvider(sourceImage));

        UInt8 *pixelData = (UInt8 *) CFDataGetBytePtr(theData);

        int red = 0;
        int green = 1;
        int blue = 2;

        int dataLength = CFDataGetLength(theData);
        for (int index = 0; index < dataLength; index += 4)
        {
            if (pixelData[index + blue] - 80 > 0)
            {
                pixelData[index + red] = pixelData[index + blue] - 139;
                pixelData[index + green] = pixelData[index + blue] - 85;
            }
            else
            {
                pixelData[index + green] = 0;
                pixelData[index + red] = 0;
            }
        }

        CGContextRef context;
        context = CGBitmapContextCreate(pixelData,
                                        CGImageGetWidth(sourceImage),
                                        CGImageGetHeight(sourceImage),
                                        8,
                                        CGImageGetBytesPerRow(sourceImage),
                                        CGImageGetColorSpace(sourceImage),
                                        kCGImageAlphaPremultipliedLast);

        CGImageRef newCGImage = CGBitmapContextCreateImage(context);
        UIImage *newImage = [UIImage imageWithCGImage:newCGImage];
        ImageView_Test.image = newImage;
        CGContextRelease(context);
        CFRelease(theData);
        CGImageRelease(newCGImage);

2 个答案:

答案 0 :(得分:2)

我正在使用以下方法来改变UIImage的颜色而不影响它的alpha。

-(UIImage *)didImageColorchanged:(NSString *)name withColor:(UIColor *)color
{
    UIImage *img = [UIImage imageNamed:name];
    UIGraphicsBeginImageContext(img.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [color setFill];
    CGContextTranslateCTM(context, 0, img.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, kCGBlendModeColorBurn);
    CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
    CGContextDrawImage(context, rect, img.CGImage);
    CGContextClipToMask(context, rect, img.CGImage);
    CGContextAddRect(context, rect);
    CGContextDrawPath(context,kCGPathFill);
    UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return coloredImg;
}

前:

resultView.image =  [self didImageColorchanged:[UIImage imageNamed:@"xyz.png"] withColor:[UIColor redColor]];

答案 1 :(得分:0)

你可以使用

 // load image
UIImage *image      = [UIImage imageNamed:@"test.png"];
CGImageRef imageRef = image.CGImage;
NSData *data        = (NSData *)CGDataProviderCopyData(CGImageGetDataProvider(imageRef));
char *pixels        = (char *)[data bytes];

// this is where you manipulate the individual pixels
// assumes a 4 byte pixel consisting of rgb and alpha
// for PNGs without transparency use i+=3 and remove int a
for(int i = 0; i < [data length]; i += 4)
{
    int r = i;
    int g = i+1;
    int b = i+2;
    int a = i+3;

    pixels[r]   = 0; // eg. remove red
    pixels[g]   = pixels[g];
    pixels[b]   = pixels[b];
    pixels[a]   = pixels[a];
}

// create a new image from the modified pixel data
size_t width                    = CGImageGetWidth(imageRef);
size_t height                   = CGImageGetHeight(imageRef);
size_t bitsPerComponent         = CGImageGetBitsPerComponent(imageRef);
size_t bitsPerPixel             = CGImageGetBitsPerPixel(imageRef);
size_t bytesPerRow              = CGImageGetBytesPerRow(imageRef);

CGColorSpaceRef colorspace      = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo         = CGImageGetBitmapInfo(imageRef);
CGDataProviderRef provider      = CGDataProviderCreateWithData(NULL, pixels, [data length], NULL);

CGImageRef newImageRef = CGImageCreate (
                          width,
                          height,
                          bitsPerComponent,
                          bitsPerPixel,
                          bytesPerRow,
                          colorspace,
                          bitmapInfo,
                          provider,
                          NULL,
                          false,
                          kCGRenderingIntentDefault
                          );
// the modified image
UIImage *newImage   = [UIImage imageWithCGImage:newImageRef];

// cleanup
free(pixels);
CGImageRelease(imageRef);
CGColorSpaceRelease(colorspace);
CGDataProviderRelease(provider);
CGImageRelease(newImageRef);