My application is a kind of picture gallery. when ever user clicks on icon in the gallery, need to display the images( Landscape 2 images , portrait 1 image). The pictures may be more than 100. I usually take raw file and decode into UIImage format. If user wants to see another image it's taking some time (delay) to display the image because of decoding. So i want save some of the images into cache(NSArray ) in a separate thread(GCD) to resolve this problem.
In array i may store 5 to 10 images. Need to update every time when ever user swipes.
Kindly give the suggestions.
Thanks in advance.
I have 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 into 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"];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With