0

我试图调用UIImagePNGRepresentation主线程以外的方法,但我只得到 EXC_BAD_ACCESS 异常。

这是代码

UIImage *imageToSave = [UIImage imageWithCGImage:imageRef];

dispatch_queue_t taskQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_async(taskQ,
               ^{
                   NSData *binaryImage = UIImagePNGRepresentation(imageToSave);

                   if (![binaryImage writeToFile:destinationFilePath atomically:YES]) {
                       SCASSERT(assCodeCannotCreateFile, @"Write of image file failed!");
                   };
               });

尝试访问 imageToSave 变量时发生异常。

编辑:我想将 OpenGL 场景保存到 PNG 文件,有整个函数的代码

- (void)saveImage:(NSString *)destinationFilePath {

NSUInteger width  = (uint)self.frame.size.width;
NSUInteger height = (uint)self.frame.size.height;

NSUInteger myDataLength = width * height * 4;

// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *)malloc(myDataLength);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

// gl renders "upside down" so swap top to bottom into new array.
// there's gotta be a better way, but this works.
GLubyte *buffer2 = (GLubyte *)malloc(myDataLength);

for (int y = 0; y <height; y++) {
    for (int x = 0; x <width * 4; x++) {
        buffer2[((int)height - 1 - y) * (int)width * 4 + x] = buffer[y * 4 * (int)width + x];
    }
}

JFFree(buffer);

// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);

// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

// make the cgimage
CGImageRef imageRef = CGImageCreate(width, 
                                    height, 
                                    bitsPerComponent, 
                                    bitsPerPixel,
                                    bytesPerRow,
                                    colorSpaceRef,
                                    bitmapInfo,
                                    provider,
                                    NULL,
                                    NO,
                                    renderingIntent);

UIImage *imageToSave = [UIImage imageWithCGImage:imageRef];

dispatch_queue_t taskQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_async(taskQ,
               ^{
                   NSData *binaryImage = UIImagePNGRepresentation(imageToSave);

                   if (![binaryImage writeToFile:destinationFilePath atomically:YES]) {
                       SCASSERT(assCodeCannotCreateFile, @"Write of image file failed!");
                   };
               });

// release resources
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
CGImageRelease(imageRef);
JFFree(buffer2);
}
4

1 回答 1

1

省略 CGImageRelease 调用,因为 UIImage 不保留 CGImageRef

imageWithCGImage 和内存

于 2012-12-28T12:41:15.650 回答