CGBitmapContextCreate() 出现此错误的原因可能是什么?

发布于 2024-10-15 15:00:58 字数 3053 浏览 6 评论 0原文

我的应用程序在控制台上给出了这些错误:

<Error>: CGBitmapContextCreateImage: invalid context 0xdf12000
Program received signal:  “EXC_BAD_ACCESS”.

来自以下代码:

- (UIImage *)pureBlackAndWhiteImage:(UIImage *)image {

//CGImageRef imageR = [image CGImage];
CGColorSpaceRef colorSpac = CGColorSpaceCreateDeviceGray(); 
//CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height); 
CGContextRef contex = CGBitmapContextCreate(NULL, image.size.width, 
                                            image.size.height, 8, 1*image.size.width, colorSpac, kCGImageAlphaNone); 
contex = malloc(image.size.height * image.size.width * 4);
    CGColorSpaceRelease(colorSpac); 
// Draw the image into the grayscale context 

//CGContextDrawImage(contex, rect, NULL); 
CGImageRef grayscale = CGBitmapContextCreateImage(contex); 
CGContextRelease(contex); 


NSUInteger width = CGImageGetWidth(grayscale);
NSUInteger height = CGImageGetHeight(grayscale);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CFDataRef datare=CGDataProviderCopyData(CGImageGetDataProvider(grayscale));



unsigned char *dataBitmap=(unsigned char *)CFDataGetBytePtr(datare);
dataBitmap = malloc(height * width * 4);
NSUInteger bytesPerPixel = 1;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(dataBitmap, width, height,
                                             bitsPerComponent, bytesPerRow, colorSpace,
                                             kCGImageAlphaNoneSkipLast);


//unsigned char *dataBitmap = [self bitmapFromImage:image];

for (int i = 0; i < image.size.width * image.size.height * 4; i += 4) {

   // if ((dataBitmap[i + 1] + dataBitmap[i + 2] + dataBitmap[i + 3]) < (255 * 3 / 2)) {

    if(dataBitmap[i+1]>128 && dataBitmap[i+2]>128 && dataBitmap[i+3]>128)
    {
        dataBitmap[i + 1] = 0;
        dataBitmap[i + 2] = 0;
        dataBitmap[i + 3] = 0;
    } else {
        dataBitmap[i + 1] = 255;
        dataBitmap[i + 2] = 255;
        dataBitmap[i + 3] = 255;
    }
}



   //CFDataRef newData=CFDataCreate(NULL,dataBitmap,length);
CGColorSpaceRef colorSpa = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapcontext = CGBitmapContextCreate(dataBitmap,
                                                   image.size.width, 
                                                   image.size.height, 
                                                   8, 
                                                   1*image.size.width, 
                                                   colorSpa, 
                                                   kCGImageAlphaNone);
CFRelease(colorSpace);
CFRelease(colorSpa);
CGImageRef cgImage = CGBitmapContextCreateImage(bitmapcontext);
CFRelease(cgImage);
CFRelease(bitmapcontext);
CGContextRelease(context);
free(dataBitmap);
//CFRelease(imageR);
UIImage *newImage = [UIImage imageWithCGImage:cgImage];
return newImage;

}

是什么导致了这些错误?

My application gives these errors on the console:

<Error>: CGBitmapContextCreateImage: invalid context 0xdf12000
Program received signal:  “EXC_BAD_ACCESS”.

from the following code:

- (UIImage *)pureBlackAndWhiteImage:(UIImage *)image {

//CGImageRef imageR = [image CGImage];
CGColorSpaceRef colorSpac = CGColorSpaceCreateDeviceGray(); 
//CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height); 
CGContextRef contex = CGBitmapContextCreate(NULL, image.size.width, 
                                            image.size.height, 8, 1*image.size.width, colorSpac, kCGImageAlphaNone); 
contex = malloc(image.size.height * image.size.width * 4);
    CGColorSpaceRelease(colorSpac); 
// Draw the image into the grayscale context 

//CGContextDrawImage(contex, rect, NULL); 
CGImageRef grayscale = CGBitmapContextCreateImage(contex); 
CGContextRelease(contex); 


NSUInteger width = CGImageGetWidth(grayscale);
NSUInteger height = CGImageGetHeight(grayscale);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CFDataRef datare=CGDataProviderCopyData(CGImageGetDataProvider(grayscale));



unsigned char *dataBitmap=(unsigned char *)CFDataGetBytePtr(datare);
dataBitmap = malloc(height * width * 4);
NSUInteger bytesPerPixel = 1;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(dataBitmap, width, height,
                                             bitsPerComponent, bytesPerRow, colorSpace,
                                             kCGImageAlphaNoneSkipLast);


//unsigned char *dataBitmap = [self bitmapFromImage:image];

for (int i = 0; i < image.size.width * image.size.height * 4; i += 4) {

   // if ((dataBitmap[i + 1] + dataBitmap[i + 2] + dataBitmap[i + 3]) < (255 * 3 / 2)) {

    if(dataBitmap[i+1]>128 && dataBitmap[i+2]>128 && dataBitmap[i+3]>128)
    {
        dataBitmap[i + 1] = 0;
        dataBitmap[i + 2] = 0;
        dataBitmap[i + 3] = 0;
    } else {
        dataBitmap[i + 1] = 255;
        dataBitmap[i + 2] = 255;
        dataBitmap[i + 3] = 255;
    }
}



   //CFDataRef newData=CFDataCreate(NULL,dataBitmap,length);
CGColorSpaceRef colorSpa = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapcontext = CGBitmapContextCreate(dataBitmap,
                                                   image.size.width, 
                                                   image.size.height, 
                                                   8, 
                                                   1*image.size.width, 
                                                   colorSpa, 
                                                   kCGImageAlphaNone);
CFRelease(colorSpace);
CFRelease(colorSpa);
CGImageRef cgImage = CGBitmapContextCreateImage(bitmapcontext);
CFRelease(cgImage);
CFRelease(bitmapcontext);
CGContextRelease(context);
free(dataBitmap);
//CFRelease(imageR);
UIImage *newImage = [UIImage imageWithCGImage:cgImage];
return newImage;

}

What could be causing these errors?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨夜星沙 2024-10-22 15:00:58

当图像的宽度远大于此值时,您尝试使用每行 4 个字节创建上下文。
如果每个像素有 1 个字节,那么您应该使用 1 * image.size.width 而不是 4 个字节,就像您第二次和第三次创建位图上下文时所做的那样。

除此之外,我认为将 imageR 作为第一个参数传递不是一个好主意。如果您要针对 iOS 4 或更高版本进行部署,则可以传递 NULL
否则,我认为你必须分配内存来存储位图上下文数据。

You're trying to create the context using 4 bytes per row, when the width of the image is a lot more than that.
If you have 1 byte per pixel, then instead of 4 you should use 1 * image.size.width, like you do the second and third times you create a bitmap context.

Besides that, I don't think passing imageR as the first argument is a good idea. If you're deploying for iOS 4 or later, you can pass NULL instead.
Otherwise, I think you have to allocate the memory to store the bitmap context data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文