Need to save and retrive UIImages using buffer
Asked Answered
S

3

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.

Salangi answered 8/4, 2015 at 9:42 Comment(5)
You should use NSCache for memory cache, or look into solutions with disk cache too. There are plenty of cache implementations for images.Mireielle
Thanks Andy. I will do the same. Kindly provide some links to refer if it is possible.Salangi
@Andy Thanks again. Implemented NSCache as you suggested. it's working fine.Salangi
Awesome, very simple right?!Mireielle
Yes. Easily can implementSalangi
Z
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];
 }
Zubkoff answered 8/4, 2015 at 9:53 Comment(1)
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)Salangi
S
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"];

}

Salangi answered 15/4, 2015 at 10:34 Comment(0)
Z
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];
 }
Zubkoff answered 8/4, 2015 at 9:53 Comment(1)
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)Salangi
C
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.

Crick answered 8/4, 2015 at 9:46 Comment(1)
Thanks for your assurance bro. is it applicable for the high resolution images?Salangi

© 2022 - 2024 — McMap. All rights reserved.