【发布时间】:2012-05-26 17:05:30
【问题描述】:
我正在尝试对图像应用多种效果。我创建了一个单独的文件来处理我可以发送UIImageView 并接收修改后的副本的效果处理。为了节省处理时间,我首先将图像加载到单独的处理文件中并存储在内存中,而不是每次要修改时都加载图像。流程是 getImageData -> modifyRGB -> displayImage。一切正常,直到最后一步。返回的修改后的图像会在屏幕上显示片刻,然后 Xcode 崩溃并出现 EXEC_BAD_ACCESS:code 1 错误。我反复检查代码,找不到问题。任何帮助是极大的赞赏。谢谢!
更新更多信息
我正在使用带有自动引用计数的 Xcode 4.3.1
使用换行符,我可以验证在执行 self.imageView.image = [self.imageManipulation displayImage]; 行时发生崩溃。图像已更新,但随后程序立即崩溃。 使用 NSZombie,我得到一个错误 -[Not A Type retain]: message sent to deallocated instance 0x2cceaf80
从我使用的 viewController 中:
[self.imageManipulation getImageData:self.imageView.image];
[self.imageManipulation modifyRGB];
self.imageView.image = [self.imageManipulation displayImage];
我的 ImageManipulation 文件包括:
@implementation ImageManipulation
static unsigned char *rgbaDataOld;
static unsigned char *rgbaDataNew;
static int width;
static int height;
- (void)getImageData:(UIImage *)image
{
CGImageRef imageRef = [image CGImage];
width = CGImageGetWidth(imageRef);
height = CGImageGetHeight(imageRef);
rgbaDataOld = malloc(height * width * 4);
rgbaDataNew = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(rgbaDataOld, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
CGImageRelease(imageRef);
}
//modify rgb values
- (void)modifyRGB
{
for (int byteIndex = 0 ; byteIndex < width * height * 4; byteIndex += 4)
{
rgbaDataNew[byteIndex] = (char) (int) (rgbaDataOld[byteIndex] / 3) + 1;
rgbaDataNew[byteIndex+1] = (char) (int) (rgbaDataOld[byteIndex+1] / 3 + 1);
rgbaDataNew[byteIndex+2] = (char) (int) (rgbaDataOld[byteIndex+2] / 3) + 1;
rgbaDataNew[byteIndex+3] = (char) (int) 255;
}
}
//set image
- (UIImage *)displayImage
{
CGContextRef context;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
context = CGBitmapContextCreate(rgbaDataNew, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast );
CGImageRef imageRef = CGBitmapContextCreateImage (context);
UIImage *outputImage = [UIImage imageWithCGImage:imageRef];
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
CGImageRelease(imageRef);
return outputImage;
}
@end
【问题讨论】:
-
一般情况下Excess_Bad_Access是在你访问被释放的数据或者对象没有引用它的时候出现。检查你的代码分配和释放对象,异常就出现了。
-
很遗憾你的问题太宽泛了。您是否设置了一些断点以在抛出错误时缩小范围?您是否在 Debug Navigator 中将线程列表压缩滑块一直滑动到右侧以在抛出异常时查看更大的堆栈块,或者在 lldb 中键入“bt”(用于“backtrace”)(或gdb)控制台?正如 roronoa 所说,这可能是由于试图访问已经发布的内容,但一些基本的调试来缩小范围会为您解答或至少给我们足够的线索来帮助。
-
我会尝试进一步缩小范围。执行 self.imageView.image = [self.imageManipulation displayImage]; 行时发生崩溃。而且,即使它在那条线上崩溃,仍然显示变暗的修改图像,但仅在崩溃前的一瞬间。 [self.imageManipulation displayImage] 方法完全执行 - 使用断点验证。我会继续检查。
-
您似乎正确使用了 imageWithCGImage ...您的 imageView 是否没有以某种方式保留?您是否在跟踪中得到任何信息?
-
不确定。这是跟踪错误:tid = 0x2203, 0x016ba09b libobjc.A.dylib`objc_msgSend + 15, stop reason = EXC_BAD_ACCESS (code=1, address=0x439a0008) 这绝对是发送的问题向不存在的对象发送消息,或者至少无法接收该特定消息。我的猜测是,图像正在通过某种仅限链接的方式进行更新。然后链接被破坏,程序崩溃。我不确定如何解决这个问题。有没有办法将图像永久分配给 UIImageView 使其独立?
标签: ios image image-processing