Note that there are some explanatory texts on larger screens.

plurals
  1. POUIImage caching with Xcode Asset Catalogs
    primarykey
    data
    text
    <p>We all know about the mysterious behind-the-scenes caching mechanism of UIImage's <code>imageNamed:</code> method. In Apple's <a href="https://developer.apple.com/library/IOS/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html" rel="noreferrer">UIImage Class Reference</a> it says:</p> <blockquote> <p>In low-memory situations, image data may be purged from a UIImage object to free up memory on the system. This purging behavior affects only the image data stored internally by the UIImage object and not the object itself. When you attempt to draw an image whose data has been purged, the image object automatically reloads the data from its original file. This extra load step, however, may incur a small performance penalty.</p> </blockquote> <p><s>In fact, image data will not be "purged from a UIImage object to free up memory on the system" as the documentation suggests, however. <strong>Instead, the app receives memory warnings until it quits "due to memory pressure".</strong></s><br> <strong>EDIT:</strong> When using the conventional image file references in your Xcode project, the UIImage caching works fine. It's just when you transition to Asset Catalogs that the memory is never released.</p> <p>I implemented a UIScrollView with a couple of UIImageViews to scroll through a long list of images. When scrolling, the next images are being loaded and assigned to the UIImageView's <code>image</code> property, removing the strong link to the UIImage it has been holding previously.</p> <p>Because of <code>imageNamed:</code>'s caching mechanism, I quickly run out of memory, though, and the app terminates with around 170 MB memory allocated.</p> <p>Of course there are plenty of interesting solutions around to implement custom caching mechanisms, including overriding the <code>imageNamed:</code> class method in a category. Often, the class method <code>imageWithContentOfFile:</code> that does not cache the image data is used instead, as even suggested by Apple developers at the WWDC 2011.</p> <p>These solutions work fine for regular image files, although you have to get the path and file extension which is not quite as elegant as I would like it to be.</p> <p>I am using the new <strong>Asset Catalogs</strong> introduced in Xcode 5, though, to make use of the mechanisms of conditionally loading images depending on the device and the efficient image file storage. As of now, there seems to be no straight forward way to load an image from an Asset Catalog without using <code>imageNamed:</code>, unless I am missing an obvious solution.</p> <p><strong>Do you guys have figured out a UIImage caching mechanism with Asset Catalogs?</strong></p> <p>I would like to implement a category on UIImage similar to the following:</p> <pre><code>static NSCache *_cache = nil; @implementation UIImage (Caching) + (UIImage *)cachedImageNamed:(NSString *)name { if (!_cache) _cache = [[NSCache alloc] init]; if (![_cache objectForKey:name]) { UIImage *image = ???; // load image from Asset Catalog without internal caching mechanism [_cache setObject:image forKey:name]; } return [_cache objectForKey:name]; } + (void)emptyCache { [_cache removeAllObjects]; } @end </code></pre> <p>Even better would of course be a way to have more control over <code>UIImage</code>'s internal cache and the possibility to purge image data on low memory conditions as described in the documentation when using Asset Catalogs.</p> <p>Thank you for reading and I look forward to your ideas!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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