0

I have a mutable array of dictionary store image from url.So how to store image in application cache and i can view image when not have internet connection.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * CellIndentifier=@"CustomCell";
    CustomCell *cell = (CustomCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIndentifier forIndexPath:indexPath];
    NSDictionary *dictVideo = [self.videoList objectAtIndex:indexPath.row];
    [cell.indicator startAnimating];
    //set title
    NSString *titleVideo = [dictVideo objectForKey:@"Title"];
    [cell.myLabel setText:titleVideo];
    // set image url
    NSString *urlVideo = [dictVideo objectForKey:@"Url"];
    NSURL *url = [NSURL URLWithString:urlVideo];
    cell.imageView.image = nil;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage * img = [[UIImage alloc]initWithData:data];
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSLog(@"%ld  %@  %@",(long)indexPath.row,titleVideo,url);
                [cell.imageView setImage: img];
                [cell.indicator stopAnimating];
        });
          });
           return cell;
    }
HIEU
  • 25
  • 8
  • Define `cache`. Your local sqlite, documents folder? You have a few options, since you already have the raw data. Example: `[[NSFileManager defaultManager] createFileAtPath:path contents:data attributes:nil];` – SwiftArchitect Jul 07 '14 at 03:24
  • i want to store image i downloaded from internet to Caches folder in document to load image more fast – HIEU Jul 07 '14 at 03:27
  • I answer a question just like this here, including using NSCache to cache image data... http://stackoverflow.com/questions/15799432/poor-uicollectionview-scrolling-performance-with-uiimage/15799771#15799771 – danh Jul 07 '14 at 03:40
  • @TimeToLearn if you want to cache image here is very good library [SDWebImage](https://github.com/rs/SDWebImage) . – ChintaN -Maddy- Ramani Jul 07 '14 at 04:05

2 Answers2

1

you can use SDWebImage library as Umar Farooq said,Its support for both iOS 6 and 7 you can then use below code in your .m file

#import "UIImageView+WebCache.h"
 UIImage *Noimg = [UIImage imageNamed:@"noimg.png"];
     [cell.PlaceImg  setImageWithURL:[NSURL URLWithString:strImgname]  placeholderImage:Noimg];

Here Noimg is the placeholder image, its automatically manage by library when there is no image found on image location

Kirtikumar A.
  • 4,140
  • 43
  • 43
0

NSURLCache already does this for you. Your application already has an implicit NSURLCache that may not have on disk persistence, so you want to explicitly set it:

NSURLCache *cache = [[NSURLCache alloc]initWithMemoryCapacity:(5 * 1024 * 1024) diskCapacity:(10 * 1024 *1024) diskPath:@"NSURLCache.db"];
[NSURLCache setSharedURLCache:cache];

And from that point on, the URL loading system will store data both in memory and on disk. This will allow it to be available when there is no connection. You continue to make requests as usual, they just return a lot faster when served out of the cache.

If you want to modify this behavior, you should modify the cache policy used by your NSURLRequest (for example, NSURLRequestReturnCacheDataElseLoad or NSURLRequestReturnCacheDataDontLoad). Unfortunately, you are using dataWithContentsOfURL: which does not provide an opportunity to do that. Using NSURLSessionTask or NSURLConnection would allow you to do so.

Keep in mind that the server does tell the client how long a response is valid for, and the default cache policy does obey that.

quellish
  • 21,123
  • 4
  • 76
  • 83