我在后台线程上调用一个方法..写在下面
imageRef = assetLbraryImp.defaultRepresentation.fullResolutionImage;
[self saveBigImageFull:imageRef withName:mediafileName addToAlbum:isFromCamera];
-(void)saveBigImageFull:(CGImageRef)bigImage withName:(NSString*)imageName addToAlbum: (BOOL)addToAlbum{
bigImageFulldata = [NSArray arrayWithObjects: imageName, [NSNumber numberWithBool:addToAlbum], nil];
[self saveBigImage:bigImageFulldata :bigImage];
}
-(void)saveBigImage:(id)data:(CGImageRef)imageRefere{
_iName = (NSString *)[data objectAtIndex:0] ;
bigImagefileName = [[NSString alloc]initWithFormat:@"%@.jpg", _iName] ;
bigImageFilepath = [photoPath stringByAppendingPathComponent:bigImagefileName] ;
[self savePhotoBig:imageRefere toPath:bigImageFilepath];
}
-(void)savePhoto:(CGImageRef)photo toPath:(NSString *)path{
image = [UIImage imageWithCGImage:photo];
imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:path atomically:NO];
if(photo)
CFRelease(photo);
}
但由于CFRelease,应用程序崩溃。当我删除CFRelease然后代码工作正常..如果我保存100图像然后100图像得到保存。即使在最终应用程序崩溃后保存。应用程序在进程结束时崩溃而不是介于两者之间。
任何想法?
答案 0 :(得分:0)
由于CFRelease,应用程序崩溃。当我删除CFRelease然后代码工作正常。
我建议您在调用photo
的方法中检查您使用savePhoto:
执行的操作;您是在致电photo
后访问savePhoto:
吗?
如果保存100张图像,则保存100张图像。即使在最终应用程序崩溃后保存。
我认为这是一个内存填充问题。使用Instruments的内存分配工具检查您的应用程序,确认它因内存填满而被杀死。
编辑:
阅读上面的代码,您似乎正在保存以下图片:
imageRef = assetLbraryImp.defaultRepresentation.fullResolutionImage;
这是您稍后通过CFRelease
发布的相同图片。这里有两件事:
可能assetLibraryImp是图像的实际所有者,因此您最好让它进行发布(但我不能从您的代码中判断出来,所以您会知道);
如果你想在savePhoto
中发布,那么我确实认为你应该使用CGImageRelease
代替CFRelease
,即:
if(photo)
CGImageRelease(photo);
此外,您说您正在后台线程中执行该方法:请注意,UIKit不是线程安全的,因此很可能这是您崩溃的原因。在主线程上尝试并执行该方法(尽管它会阻止UI,仅用于测试)并查看会发生什么。