简体   繁体   中英

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?

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.

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM