简体   繁体   中英

Unable to change the colour of pixel in UIImage

I got my problem solved already by using a different code. i just want to know what is wrong with the following one?

I wanted to change colour of every pixel in UIImage using bitmap data. My code is as follows:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIImage *image = self.imageViewMain.image;

    CGImageRef imageRef = image.CGImage;
    NSData *data        = (NSData *)CFBridgingRelease(CGDataProviderCopyData(CGImageGetDataProvider(imageRef)));
    char *pixels        = (char *)[data bytes];

    // this is where we manipulate the individual pixels
    for(int i = 1; i < [data length]; i += 3)
    {
        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);
}

But when this code runs - I get EXC_BAD_ACCESS error shown like in the following image :

在此处输入图片说明

And here is some more information from debugging:

在此处输入图片说明

What is it that I'm missing or doing wrong ?

try to alloc memory for pixels array like following code

char *pixels = (char *)malloc(data.length);
memcpy(pixels, [data bytes], data.length);

when pixels is not necessary, release this memory by call free(pixels)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM