Iphone 尝试在后台保存图像
我试图在主线程之外的其他线程中调用Iphone 尝试在后台保存图像,iphone,ios,Iphone,Ios,我试图在主线程之外的其他线程中调用UIImagePNGRepresentation,但只得到EXC\u BAD\u访问异常 这是代码 UIImage *imageToSave = [UIImage imageWithCGImage:imageRef]; dispatch_queue_t taskQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(taskQ,
UIImagePNGRepresentation
,但只得到EXC\u BAD\u访问异常
这是代码
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变量时发生异常
编辑:
我想把OpenGLSCeene保存到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);
}
-(void)saveImage:(NSString*)destinationFilePath{
NSUInteger width=(uint)self.frame.size.width;
NSU整数高度=(uint)self.frame.size.height;
NSUInteger myDataLength=宽度*高度*4;
//分配数组并将像素读入其中。
GLubyte*buffer=(GLubyte*)malloc(myDataLength);
glReadPixels(0,0,宽度,高度,GL_RGBA,GL_无符号字节,缓冲区);
//gl呈现“倒置”,以便从上到下交换到新阵列中。
//肯定有更好的办法,但这是可行的。
GLubyte*buffer2=(GLubyte*)malloc(myDataLength);
对于(int y=0;y省略CGImageRelease调用,因为UIImage不保留CGImageRef
请参见检查您的imageToSave是否为零???您是否使用ARC?我使用的是ARC,imageToSave不是零。它是UIImage对象。我检查了它在主线程上时是否工作?是的,就是这样。当我在调度\u async
中移动“//release resources”之后的所有释放代码时,它工作了。感谢您的帮助