Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to save and retrive UIImages using buffer

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.

like image 892
Kiran Kumar Avatar asked Apr 08 '15 09:04

Kiran Kumar


1 Answers

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"];

}

like image 167
Kiran Kumar Avatar answered Oct 10 '22 14:10

Kiran Kumar