1

i have project with TableView, gallery and others. So i need when i open my app all images from JSON must to be save, and when i open app without internet my images must show. How it's create? What i must to do? Also i have part of my code for show images in tableViewCell to TableView:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    CellForNewsContent *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    CBAutoScrollLabel *captionLabel = [[CBAutoScrollLabel alloc] initWithFrame:CGRectMake(85, 10, 210, 20)];

    UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(85, 39, 220.0, 20.0)];

    detailLabel.tag = 2;
    captionLabel.tag = 1;
    captionLabel.text = [_news[indexPath.row] valueForKey:@"title"];
    captionLabel.scrollSpeed = 7;
    [cell addSubview:captionLabel];
    [cell addSubview:detailLabel];

    UILabel *detailShow = (UILabel*)[cell viewWithTag:2];

    detailShow.text = [[_news objectAtIndex:indexPath.row]objectForKey:@"date"];

    [cell.imageView setImageWithURL:[NSURL URLWithString:[[_news objectAtIndex:indexPath.row] valueForKey:@"smallimg"]] placeholderImage:[UIImage imageNamed:@"[email protected]"]];

Thanks.

Added JSON for image:

sliderurl : "http://site/m/miss/load/slider/3c01c445e13582c874bc56619ca47bb8.png"
1
sliderurl : "http://site/m/miss/load/slider/ba17268fa8d7d0f36fa35055960528fd.png"
2
sliderurl : "http://site/m/miss/load/slider/031b22e15228800b22105d4b97043681.png"
3
sliderurl : "http://site/m/miss/load/slider/bf484ddd41f02f8b762b517557760bfb.png"
4
sliderurl : "http://site/m/miss/load/slider/6718b988c4cf5cfb9f40219833d7118f.png"
5
sliderurl : "http://site/m/miss/load/slider/8593df80af29fe8014f9fc176991cb07.png"
6
sliderurl : "http://site/m/miss/load/slider/bf80cff0b085c63ff0416f7118659df6.png"
7
sliderurl : "http://site/m/miss/load/slider/45fbe8a52d174e145b3e98dbf9fa960c.png"
6
  • What does the JSON containing the images look like? Is the image provided as a URL or a base 64 encoded data? Commented Apr 12, 2014 at 15:17
  • JSON have key for display image, and parse from JSON also via URL Commented Apr 12, 2014 at 15:18
  • Can you add some example JSON to the question please :) Commented Apr 12, 2014 at 15:19
  • I can not find nothing about it, only about text but bot for image Commented Apr 12, 2014 at 15:19
  • You must have a snippet of the JSON you are parsing, no? Commented Apr 12, 2014 at 15:20

1 Answer 1

1

You want to download and cache the images in from the JSON. I recomend using SDWebImage.

You can either either start async downloading of all the images when you obtain the JSON or lazily load them as they are displayed. Doing the later would mean that if your images hadn't yet been viewed it wouldn't be availble offline.

To download them all when the JSON is obtained:

for (NSDictionary *item in _news) { NSString *url = item[@"sliderurl"]; [SDWebImageDownloader.sharedDownloader downloadImageWithURL:url options:0 progress:nil completed:nil]; }

Then in your table view datasource method:

id url = _news[indexPath.row][@"sliderurl"]; if ([url isEqualTo:[NSNull null]]) { // Use placeholder image if no image is given cell.imageView.image = [UIImage imageNamed:@"noholder2.png"]; } else { [cell.imageView setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"noholder2.png"]]; }

The first code snippet will download all the images and will be cached. Then the images will try to be request again when the cells are loaded - if the images has already been downloaded the cached image is used.

Sign up to request clarification or add additional context in comments.

12 Comments

I ll try now, and after i write my result
Are you using Cocapods? They make adding libraries like very simple!
Oh I just noticed that in your code you already might be using SDWebImage anyway, so adding this in should be very easy :)
I already used SDWebImage, see on structure placeholder image.
Hm i have error..2014-04-12 18:57:31.494 app[18698:70b] -[__NSCFString absoluteURL]: unrecognized selector sent to instance 0xb244e50 2014-04-12 18:57:31.499 app[18698:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString absoluteURL]: unrecognized selector sent to instance 0xb244e50' *** First throw call stack:
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.