Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The previous comment is accurate; please think about how you're going to scale this up.</p> <p>That said, you'll need to store and load images in some way, and that will always mean converting your UIImage objects to NSData. This applies whether you store them in NSUserDefaults, in files or in Core Data.</p> <p>UIImage supports NSCoding, so you could use that. Read about how to use it, since you'll need it eventually. Or, if you know you'll always want to use PNG or JPEG formats, there are the functions <code>UIImagePNGRepresentation(UIImage *image)</code> and <code>UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality)</code>. To convert the NSData objects back to UIImage, use <code>[UIImage imageWithData:NSData*]</code> Here's what you could have in applicationDidEnterBackground:</p> <pre><code>NSMutableArray *dataArray = [NSMutableArray array]; [dataArray addObject:UIImagePNGRepresentation(imageView.image)]; [dataArray addObject:UIImagePNGRepresentation(imageView2.image)]; [dataArray addObject:UIImagePNGRepresentation(imageView3.image)]; [self.user setObject:dataArray forKey:@"images"]; [self.user synchronize]; </code></pre> <p>Then, to retrieve these during viewDidLoad:</p> <pre><code>[self.array removeAllObjects]; NSArray *dataArray = [self.user objectForKey:@"images"]; for (NSData *imageData in dataArray) [self.array addObject:[UIImage imageWithData:imageData]]; </code></pre> <p>This will solve your immediate problem, but please don't consider it a permanent solution. Consider:</p> <ol> <li>Store your images in files or Core Data, as NSUserDefaults is not supposed to have large amounts of content.</li> <li>Use a UITableView to display the images, so you can have an arbitrary number instead of hard-coding for three.</li> <li>If UITableView doesn't have the layout you need, create a custom UIView subclass that displays an array of images and responds to taps appropriately.</li> <li>Make sure your application can detect all the ways it may be suspended or shut down. You may not always get the ApplicationDidEnterBackground notification, or you may not have time to save all your data after it happens. If you have multiple UIViewControllers, this one may be unloaded without the application itself receiving notifications.</li> </ol>
 

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