【发布时间】:2023-03-14 04:23:01
【问题描述】:
我有一个方法需要逐个像素地解析一堆大的 PNG 图像(每个 PNG 是 600x600 像素)。它在模拟器上似乎工作得很好,但在设备(iPad)上,我在一些内存复制功能中得到了 EXC_BAD_ACCESS。似乎尺寸是罪魁祸首,因为如果我在较小的图像上尝试它,一切似乎都有效。下面是与内存相关的方法。
+ (CGRect) getAlphaBoundsForUImage: (UIImage*) image
{
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
memset(rawData,0,height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
/* non-memory related stuff */
free(rawData);
当我在一堆图像上运行它时,它运行了 12 次然后出现问题,而在模拟器上它运行没有问题。大家有什么想法吗?
【问题讨论】:
标签: iphone memory ipad cgcontext