Assignment #5 Task #4

Please note, this blog entry is from a previous course. You might want to check out the current one.

Keep as little of the photos’ image data in memory (i.e. RAM) as you can (i.e. don’t keep strong pointers to NSData and/or UIImage objects for photos that are not on- screen). You should have at most 2 photos’ image data in memory at a given time, preferably only 1 (or even 0 if none are being viewed).

The current code up to this task does not keep any strong pointers to NSData or NSImage objects as the image is only stored in the image view. Because the iPad uses a single image view there is at most only one image in memory. The iPhone has tow image views thus there could be two images in memory.

Using the profiler proves that behavior. Using the iPad in landscape mode and selecting rapidly several photos so that they are loaded in parallel memory usage increases. As soon as a download has finished, the data is moved into the image view and thus the memory usage decreases again. However the clicked images pop up one after another.

To avoid this we create local copy of the photo and compare its ID with the the ID of the photo which is stored in the model at the time of display, and display the image only if it is the last selected.

NSDictionary *photo = [ copy];
if (self.imageView.window && 
    [[ objectForKey:FLICKR_PHOTO_ID] 
     isEqualToString:[photo objectForKey:FLICKR_PHOTO_ID]]) 
            dispatch_async(dispatch_get_main_queue(), ^{

To avoid the two images in memory described above for the iPhone we can remove it as soon as the image view disappears from screen:

- (void)viewDidDisappear:(BOOL)animated
    [super viewDidDisappear:animated];
    self.imageView.image = nil;

The complete code for this task is available at github.


Leave a Reply

Your email address will not be published.