6

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.

Kiran Kumar
  • 677
  • 3
  • 16

3 Answers3

3

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

}

Kiran Kumar
  • 677
  • 3
  • 16
2

try this one -

    UIImage *image = [imageCache objectForKey:@"myImage"];
    if (!image)
    {
      // download image
      dispatch_async(dispatch_get_global_queue(0, 0), ^{
      NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:yourURL]];
      if (imageData)
      {
        // Set image to cache
        [imageCache setObject: [UIImage imageWithData:imageData] forKey:@"myImage"];
        dispatch_async(dispatch_get_main_queue(), ^{
        [yourImageView setImage:[UIImage imageWithData:imageData]];
       });
      }
    });
 }
 else
 {
   // Use image from cache
   [yourImageView setImage:image];
 }
Vinayk
  • 194
  • 1
  • 8
  • Thanks for the reply bro. 1)The delay is not while downloading but decoding (other format to UIImage format). 2) for the imageCache which one is good (NSArray or NSMutable Array or any other one) – Kiran Kumar Apr 08 '15 at 10:06
0

You can use LazyTableImages from Apple.

This code will download the image in background and set to imageview when download completes. You can add placeholder image until your original image is in downloading process.

I used this sample in many application so i can also help you in implementation if you required.

Hope this will help you.

Malav Soni
  • 2,270
  • 16
  • 32