ios save Image Offline - ios

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 ?

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]

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.

Related

Data is nil from image string.

It is my save method :
-(NSString *)saveImage:(UIImage *)image
{
NSInteger RandomIndex = arc4random() % 1000;
NSString *randomImageName =[NSString stringWithFormat:#"Image%i.png",RandomIndex];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:randomImageName];
if ([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]) {
[[NSFileManager defaultManager] removeItemAtPath:savedImagePath error:nil];
NSLog(#"file removed from path");
}
NSLog(#"Saved Image Path : %#",savedImagePath);
NSData* imageData = UIImagePNGRepresentation (image);
[imageData writeToFile:savedImagePath atomically:YES];
self.teamLogo = savedImagePath;
return savedImagePath;
}
I try to load image and put into control :
NSData *myData = [NSData dataWithContentsOfFile:self.team.logo];
UIImage *selectedImage = [UIImage imageWithData:myData];
self.team.logo is from DB. I keep string in base. In debug mode i got this string :
/var/mobile/Containers/Data/Application/7CD69EC9-9C20-48EE-B611-FC2353BC31B0/Documents/Image364.png
and myData is still nil. Do you have idea why ?
Starting from IOS 8 layout of containers changed. So don't store full path to your DB. Instead just store your image name. For recalling the image use:
NSString *temp = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:#"Image364.png"];
NSData *myData = [NSData dataWithContentsOfFile:temp];
UIImage *selectedImage = [UIImage imageWithData:myData];
I hope it helps.

How to save image to iCloud using CoreData with MagicalRecord

I'm working for updating one of my app to use iCloud. all works perfectly, except for images. till now I saved the file path of the image in that way:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString*imageName = [[NSString alloc] initWithFormat:#"%#", self.fieldNome.text];
NSString *pngFilePath = [NSString stringWithFormat:#"%#/%#.png",documentsDirectory, imageName];
UIImage *image = self.myImageView.image;
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data1 writeToFile:pngFilePath atomically:NO];
I think that to synchronize images between devices I have to save images to the ubiquitary container, but I can't figure out how to get the path and ho to save there. somebody could help me?

iOS : Saving images to app folder and accessing it

I am trying to take an image and place that image to imageView, I am able to do this. But I am not able to save this to device, I want my photo to be saved in my application folder and want to access it from another view.
studentImage is my UIImageView element where I am able to set the image taken from camera.
Here is the code I am working with :
NSString* format = #".png";
NSString* imagename = [NSString stringWithFormat:#"%#%#",nickname,format];
UIImage *img = [self.studentImage image];
NSData *pngData = UIImagePNGRepresentation(img);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:imagename];
[pngData writeToFile:filePath atomically:YES];
This statement is returning null : [pngData writeToFile:filePath atomically:YES];
It would be great, if some one can tell me how to access that image from another view.

My images have been deleted after an app update

I'm trying to update my iPad app using TestFlight and the problem I have is that the images which I'm storing are being deleted when I update the app.
I'm using this code to downloading and later store the images:
NSData *responseData = [request responseData];
UIImage *imageURL = [[UIImage alloc] initWithData:responseData];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(imageURL)];
[data1 writeToFile:documentsDirectory atomically:YES];
[imageURL release];
You are saving your image directly over (not inside) Documents directory. It should not even save the images in the first place. Instead create a subpath under Documents directory and save your file there.
NSString* imagePath = [documentsDirectory stringByAppendingPathComponent:#"someImage.png"];
[data1 writeToFile:imagePath atomically:YES];
The link which at the end solves my trouble was:
Problem with NSSearchPathForDirectoriesInDomains And Persistent Data
Thanks anyway!!

Displaying Saved Images in iOS 5

I have an app that I am saving images to the NSDocumentDirectory. I can see that the path is being set correctly but when I try to retrieve them the NSData is null.
Here are some of my snippets..
NSData *imageData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"myApp"];
//Create unique filename
CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef newUniqueIdString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);
path = [path stringByAppendingPathComponent:(__bridge_transfer NSString *)newUniqueIdString];
path = [path stringByAppendingPathExtension: #"png"];
[imageData writeToFile:path atomically:NO];
[self showPhoto:path];
Then in my showPhoto method
NSData *pngData = [NSData dataWithContentsOfFile:path];
UIImage *image = [UIImage imageWithData:pngData];
If I debug pngData it is 0 bytes but my path show what I think is the correct path?
Any way of debugging if the photo is actually on the phone? Im not sure if it is getting saved or not.
You can do something like this and check if the file exists first and use imageWithContentsOfFile: to retrieve it.
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:localFilePath];
bool fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imagePath];
if(fileExists == YES)
{
UIImage *_image = [UIImage imageWithContentsOfFile:localFilePath]
}
If the image is still empty then there is something wrong with your image.
If you running app in simulator you can find application files here: /Users/{user}/Library/Application Support/iPhone Simulator/5.0/Applications
If you debugging on iPhone you can use iExplorer to see files on iPhone.

Resources