我正在尝试在除主线程之外的其他地方调用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);
}

最佳答案

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

imageWithCGImage and memory

关于iphone - 尝试在背景中保存图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14069595/

10-10 21:34
查看更多