【问题标题】:Unable to change the colour of pixel in UIImage无法更改 UIImage 中像素的颜色
【发布时间】:2016-03-08 03:34:05
【问题描述】:

我已经通过使用不同的代码解决了我的问题。我只是想知道下面这个有什么问题?

我想使用位图数据更改UIImage 中每个像素的颜色。我的代码如下:

- (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);
}

但是当这段代码运行时 - 我得到EXC_BAD_ACCESS error 如下图所示:

这里还有一些来自调试的更多信息:

我错过了什么或做错了什么?

【问题讨论】:

  • 我不认为你应该改变从[data bytes]得到的数组
  • @dan :好的,但是为什么我们不能,它只是一个数组。不是 NSArray 对象?
  • 这是一个支持不可变对象的数组。它也被声明为const。如果要修改它,请使用 NSMutableData 上的 mutableBytes 方法
  • @dan : 明白了.. 你能回答我吗?

标签: ios objective-c uiimage core-graphics


【解决方案1】:

尝试像下面的代码为像素数组分配内存

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

当不需要像素时,调用free(pixels)释放这个内存

【讨论】:

  • 有什么帮助,但是,在这个语句“像素 [a] = 像素 [a];”中给了我同样的错误值如下:r = 3055613, g = 3055614, b = 3055615, a = 3055616
  • @anoXomus 我不知道您使用的是什么算法,但是您似乎确实循环到内存不足。您的数据长度可能是 3055616。因此,从索引 0 到 3055615 的像素内容对象,当您访问像素 [3055616] 时,它会耗尽内存!
  • @anoXomus 我认为在您的算法中,您需要将数据从数据字节复制到像素数组。所以你可以像我编辑答案一样使用 memcpy
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-30
相关资源
最近更新 更多