NSCodingを使用して、いくつかの異なるフィールド(そのうちの1つは画像)を持つカスタムオブジェクトを管理しています。iOS 5.0以降のみを対象としており、ARCに切り替えました。私はすべてが機能していますが、パフォーマンスに重点を置いています。このような質問は見たことがないので、次のようにします。
UIImageをNSDataに変換し、それをメインのNSCodingファイル(重要な場合はplist)に追加して、ディスクに保存します。複数の画像がある場合、画像名は順番になります(たとえば、image1、image2、image3)。次に、UITableView(サイズ変更されたサムネイルとして)と詳細ビューの両方で画像を使用します。これのマイナス面は、plistのサイズが膨らむことです。これは、すべてのNSDataを一度にロードするため、使用すると初期ロード時間が遅くなることを意味します。
この問題を解消し、一度に1つの画像のみを強制的にロードするための最良の方法は何ですか?
私が考えたこと:
NSDataをディスクに書き込み、配列をplistに追加し、各イメージのファイル名への参照のみを配列に追加します。次に、指定された位置にある画像のファイル名を参照し、ディスク上で見つけて使用すると思いますか?
ありとあらゆる考えが大歓迎です。私は他の何よりも概念的な実装に固執しており、おかしなことに、これは頻繁に議論されるトピックではありません。
ありがとう、
編集:
以下でリクエストされているように、画像を取得してNSDataに変換する例を次に示します。
UIImage *originalImage;
NSData *imageData = UIImagePNGRepresentation(originalImage);
//I save it all to the app's document directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//I am using the images in a tableView and thus found it easiest to append the row number to each image filename.
//'y' below is just an integer that corresponds to the number of items in the master array
NSString *fileName = [NSString stringWithFormat:@"image%d.png",y];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:fileName];
[imageData writeToFile:documentsDirectory atomically:YES];
// NSLog(@"The filename is %@", fileName);
//newObject is an instance of my NSCoding object
[newObject setImageName: fileName];