My images have been deleted after an app update - ios

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!!

Related

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 ?
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.

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?

How to save multiple xml files in cache file path in ios?

Im saving xml file in cache file path as follows:
// Determile cache file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
filePathl = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"list.xml"];
// Download and write to file
NSURL *url = [NSURL URLWithString:detail_product_listing_rss];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePathl atomically:YES];
But using this code i can retrive data of recently used xml file.Could anybody please tell me how to save multiple xml files for offline use?
You need to save the xml with different names. Else it'll overwrite old xml.
Keep a integer value for this purpose, if you want this after the app restart also keep it in NSUserDefaults.
int posValue = [[[NSUserDefaults standardUserDefaults] objectForKey:#"lastXml"] intValue];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
filePathl = [NSString stringWithFormat:#"%#/list_%d.xml", [paths objectAtIndex:0],posValue];
NSURL *url = [NSURL URLWithString:detail_product_listing_rss];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePathl atomically:YES];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:posValue+1] forKey:#"lastXml"];

Loading JSON Data first then simulating-iOS

Imagine that you are reading json data from the server continuously. let's say, you are getting weather data. I want to show only data 2 hours back from now. Whenever user clicks on the play button, it would show weather data on the map.
How I could save json data first then show it on the map. Because that would enhance my application instead of connecting server , getting data and showing it at the same time. please just give me advice. Do I need to save it first as a plist or an array? Where should I keep this data before I simulate?
Storing it in the plist in the documents directory is a good way of saving data for later reference.If you want to save the JSON string you can also opt for NSUSerDefaults..(preferably if the string is not too large)
Saving to plist..
NSURL *url = [NSURL URLWithString:serverPath];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/myplist.plist", documentsDirectory];
[urlData writeToFile:filePath atomically:YES];
}
and retrieving it..
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: #"%#/myplist.plist",documentsDirectory ] ];
NSData *retrievedData = [NSData dataWithContentsOfFile:filePath];

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