I implemented NSCache using GCD
dispatch_async(dispatch_get_global_queue(0, 0), ^{ [self storeInCache]; dispatch_async(dispatch_get_main_queue(), ^{ UIImage *image=[_imageCache objectForKey:@"P5"]; self.imageView = [[UIImageView alloc] initWithImage:image]; self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size}; [self.scrollView addSubview:self.imageView]; self.scrollView.contentSize = image.size; }); });
// Store 5 images in NSCache
-(void)storeInCache { UIImage *image = [UIImage imageNamed:@"photo1.png"]; [_imageCache setObject:image forKey:@"P1"]; UIImage *image2 = [UIImage imageNamed:@"photo2.png"]; [_imageCache setObject:image2 forKey:@"P2"]; UIImage *image3 = [UIImage imageNamed:@"photo3.png"]; [_imageCache setObject:image3 forKey:@"P3"]; UIImage *image4 = [UIImage imageNamed:@"photo4.png"]; [_imageCache setObject:image4 forKey:@"P4"]; UIImage *image5 = [UIImage imageNamed:@"photo5.png"]; [_imageCache setObject:image5 forKey:@"P5"];
}
source share