Grim
Grim

Reputation: 3

ios save Image Offline

I have an application that receive images with webservices and show them in a list like for example a movie theater schedule

My question is : Is it possible to store the images in core data or something else so i can show them when the user is not connected to internet ?

Upvotes: 0

Views: 599

Answers (2)

Sreejith
Sreejith

Reputation: 1355

Yes you can.

// 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 later

UIImage *image = [UIImage imageWithData:imageData]

Upvotes: 2

David Ansermot
David Ansermot

Reputation: 6112

Yes it's possible.

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

Upvotes: 1

Related Questions