Aashi
Aashi

Reputation: 170

How to Store the online images and data for offline purpose

I have to build an iPhone app that check for the net connectivity and whenever it get it has an online sync with a web service, it downloads X images and saves them into the device.

Then, in offline mode, I have to load the images in collectionview for that i have to store images somewhere.Same For data also

I am using .net webservice with json response.

I was thinking about the Core Data storage, is that possible? Maybe storing images in core data database will slow down the app?

Upvotes: 1

Views: 1704

Answers (3)

Asad Khan
Asad Khan

Reputation: 375

Yes you can store images in core data in the form of Binary Data, here is an example of how to do it link! As far as speed is concerned core data will not slow down your app performance but it usually takes more space than sqlite, here's a link! that show comparison between core data and sqlite performance.

Upvotes: 0

zhubch
zhubch

Reputation: 194

Yes,it is possible,but not worthwhile.

As usually,we cache an image with follow steps:

  1. Check whether we have load the image at the specific URL,if yes,go step 2,otherwise,go step 3
  2. Load the image from file.
  3. DownLoad the image,and save it to file.you should give the image file a name associated with its URL,so you can find it by the url next time.

There is a tool can do bellow work well,you can try using SDWebImage.

Upvotes: 0

user3432164
user3432164

Reputation:

You have to download and save each received image in the application directory, then you save in CoreData the path to those images.

// Save image to disk
    NSString *documentaryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@/Image.png",documentaryPath];
    NSData *data = [NSData dataWithData:UIImagePNGRepresentation(YOUR_IMAGE)];
    [data writeToFile:filePath atomically:YES];

// Retrieve the Image
- (NSData *) imageData {

    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *pngFilePath = [NSString stringWithFormat:@"%@/Image.png",docDir];

    NSData *dataImage = [NSData dataWithContentsOfFile:pngFilePath];

    return dataImage;
}

And use like below.

UIImage *image = [UIImage imageWithData:imageData]

May be it will help you.

Upvotes: 5

Related Questions