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

UIImage は CGImageRef を保持しないため、CGImageRelease 呼び出しを省略します。

imageWithCGImage とメモリを参照

于 2012-12-28T12:41:15.650 に答える