CGBitmapContextCreate() 出现此错误的原因可能是什么?
我的应用程序在控制台上给出了这些错误:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当图像的宽度远大于此值时,您尝试使用每行 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 passNULL
instead.Otherwise, I think you have to allocate the memory to store the bitmap context data.