1

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

但我想将所有像素阵列的图像更改为 perticuler 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);
4

2 回答 2

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]];
于 2015-04-22T08:41:59.283 回答
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);
于 2013-12-17T08:28:04.290 回答