私は自分のアプリでこの問題を抱えていました。保存するとハングするので、GrandCentralディスパッチを使用しました。
以下は私の画像キャッシュクラスからのsetImageメソッドです。UIImageに画像がある場合はそれを保存し、そうでない場合は削除します。うまくいけば、これをニーズに合わせて調整できます。iOS4以降でのみ機能します。コードはARC対応です。
-(void)setImage:(UIImage *)image{
if (image == nil){
NSLog(@"Deleting Image");
// Since we have no image let's remove the cached image if it exists
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask, YES) objectAtIndex:0];
[[NSFileManager defaultManager] removeItemAtPath:[cachePath
stringByAppendingPathComponent:@"capturedimage.jpg"] error:nil];
});
}
else {
NSLog(@"Saving Image");
// We've got an image, let's save it to flash memory.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *cachePath =
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask, YES) objectAtIndex:0];
NSData *dataObj = UIImagePNGRepresentation(image);
[dataObj writeToFile:[cachePath
stringByAppendingPathComponent:@"capturedimage.jpg"] atomically:NO];
});
}
imageCache = image;
}