toa*_*tie 6 memory iphone cgcontext ipad
我有一个方法需要逐个像素地解析一堆大的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);
Run Code Online (Sandbox Code Playgroud)
当我在一堆图像上运行它时,它运行12次然后缩小,而在模拟器上运行没有问题.你们有什么想法吗?
我在 iPad(当然是 iPhoneOS 3.2)上使用 CGImageCreate() 时遇到了类似的崩溃。看到你的困难给了我一个暗示。我通过将 bytesPerRow 与下一个最大的 2 次幂对齐来解决了这个问题。
size_t bytesPerRowPower2 = (size_t) round( pow( 2.0, trunc( log((double) bytesPerRow) / log(2.0) ) + 1.0 ) );
如果提供 2 行对齐功能也能解决您的问题,请告诉我们。您需要分配 *rawData 调整后的大小并将 bytesPerRowPower2 传递给 CGBitmapContextCreate() ...高度似乎不需要对齐。
| 归档时间: |
|
| 查看次数: |
11884 次 |
| 最近记录: |