Graphics Reference
In-Depth Information
numberOfItemsInSection:( NSInteger )section
{
return [ self . imagePaths count ];
}
- ( UIImage *)loadImageAtIndex:( NSUInteger )index
{
//set up cache
static NSCache *cache = nil ;
if (!cache)
{
cache = [[ NSCache alloc ] init ];
}
//if already cached, return immediately
UIImage *image = [cache objectForKey : @( index ) ];
if (image)
{
return [image isKindOfClass :[ NSNull class ]]? nil : image;
}
//set placeholder to avoid reloading image multiple times
[cache setObject :[ NSNull null ] forKey : @( index ) ];
//switch to background thread
dispatch_async (
dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_LOW , 0 ), ^{
//load image
NSString *imagePath = self . imagePaths [index];
UIImage *image = [ UIImage imageWithContentsOfFile :imagePath];
//redraw image using device context
UIGraphicsBeginImageContextWithOptions (image. size , YES , 0 );
[image drawAtPoint : CGPointZero ];
image = UIGraphicsGetImageFromCurrentImageContext ();
UIGraphicsEndImageContext ();
//set image for correct image view
dispatch_async ( dispatch_get_main_queue (), ^{
//cache the image
[cache setObject :image forKey : @( index ) ];
//display the image
NSIndexPath *indexPath = [ NSIndexPath indexPathForItem :
Search WWH ::




Custom Search