私のアプリでは、使用するイメージをコードで作成する必要があります。画像の全画面表示バージョンをダウンロードしてから、必要なすべての切り抜きを作成します。このアプローチにより、ユーザーがサーバーからダウンロードする MB が削減されます。ダウンロードされる画像は約 300 です。サイズは、iPad 非 Retina では 1024x768、iPad Retina では 2048x1536 です。
これは、必要なクリッピングを作成するために画像のサイズを変更するために使用するアルゴリズムです。画像ごとに別の縮小バージョンが作成され、Document フォルダーに保存されます。
私はARCとGCDを使用しています。
CGFloat screenScale = [UIScreen mainScreen].scale;
NSString * imagePath = [[NSBundle mainBundle]pathForResource:imageName ofType:nil];
UIImage * image = [UIImage imageWithContentsOfFile:imagePath];
CGSize newImageSize = [image resizeImageSettingWidth:(380.0f * screenScale)];
UIGraphicsBeginImageContext(newImageSize);
[image drawInRect:CGRectMake(0,0,newImageSize.width,newImageSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *jpegData = UIImageJPEGRepresentation(newImage, 0.9f);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString * newImageName;
if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&
([UIScreen mainScreen].scale == 2.0)) {
newImageName = [NSString stringWithFormat:@"%@-article@2x.jpg", [imageName stringByDeletingPathExtension]];
}
else {
newImageName = [NSString stringWithFormat:@"%@-article.jpg", [imageName stringByDeletingPathExtension]];
}
NSString *filePath = [documentsPath stringByAppendingPathComponent:newImageName];
[jpegData writeToFile:filePath atomically:YES];
不明な回数のサイズ変更の後、おそらくメモリ警告のためにアプリがクラッシュします。クラッシュは iPad 第 4 世代でのみ発生し、iPad2 では完全に機能します。おそらく問題は、autorelease オブジェクトのリリースが遅く、これによりメモリが蓄積されることでしょうか?