jdruid
jdruid

Reputation: 613

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.

Upvotes: 0

Views: 866

Answers (2)

Mindaugas
Mindaugas

Reputation: 137

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.

Upvotes: 1

Jiho Kang
Jiho Kang

Reputation: 2492

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.

Upvotes: 1

Related Questions