Note that there are some explanatory texts on larger screens.

plurals
  1. PONSCoding, NSData, UIImages and performance
    text
    copied!<p>I'm using NSCoding to manage a custom object with a few different fields, one of them images. I'm only targeting iOS 5.0+ and have switched to ARC. I have everything working but am focused on performance--I haven't seen a question like this asked, so here it goes:</p> <p>I transform the UIImage into NSData and add it to the main NSCoding file (a plist, if it matters) for storage on the disk. If there is more than one image, the image names become sequential (e.g. image1, image2, image3.) I then use the image both in a UITableView (as a resized thumbnail) and in a detail view. The negative side to this is that the plist balloons in size, which means slow initial load times when I use it because it's loading all of the NSData at once.</p> <p>What is the best way to eliminate this problem and only force the loading of one image at a time?</p> <p><strong>What I've thought of:</strong> </p> <p>I write the NSData to the disk, add an array to the plist, and add only a reference to the filename of each image to the array. I suppose I'd then reference the image filename at specified position, find it on the disk, and use it? </p> <p>Any and all thoughts would be most welcome. I'm more stuck on the conceptual implementation than anything else and, funnily enough, this is not an oft-discussed topic.</p> <p>Thanks, </p> <p><strong>EDIT:</strong> </p> <p>As requested below, here's an example of taking an image and turning it into NSData:</p> <pre><code> 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]; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload