Jack Nutkins
Jack Nutkins

Reputation: 1555

objective-c: failed to write image to documents directory

I'm using this piece of code to try and save an image to the documents directory:

//FOR TESTING ON SIMULATOR 
UIImage* image = [UIImage imageNamed:@"ReceiptImage1.jpg"];
NSData *imageData = UIImagePNGRepresentation(image);
NSString* imageName = @"Receipt1Image1.png";
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];        
[imageData writeToFile:fullPathToFile atomically:NO];

self.receiptImage1 = fullPathToFile;

self.receiptImageView1.image = [UIImage imageWithContentsOfFile:fullPathToFile];

NSLog(@"fullPathToFile %@", fullPathToFile);

However this piece of code doesn't set the imageViews image to the receipt image that I'm trying to write to the documents directory.

Can someone explain what I'm doing wrong and how to fix it?

Thanks,

Jack

Upvotes: 1

Views: 4598

Answers (4)

Fernando Cervantes
Fernando Cervantes

Reputation: 2962

You could take the following approach

-(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
    if ([[extension lowercaseString] isEqualToString:@"png"]) {
        [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
    } else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
        [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
    } else {
        ALog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);
    }
}

To save an image, simply do it like this:

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[self saveImage:yourImage withFileName:@"Your Image Name" ofType:@"png" inDirectory:path];

To load the image, implement this method:

-(UIImage *) loadImage:(NSString *)fileName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {

    UIImage * result = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.%@", directoryPath, fileName, extension]];

    return result;
}

And then use it like this:

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
yourImage.image = [self loadImage:@"Your Image Name" ofType:@"png" inDirectory:path];

Or you could simply set your image equal to what this method returns instead of creating a method:

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
yourImage.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/Your Image Name.png", path]];

Upvotes: 0

horseshoe7
horseshoe7

Reputation: 2817

It may not save to that path if there is already a file present. (Check the API docs, I can't remember exactly. But to be save you should add something like:

    NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectoryPath = [dirs objectAtIndex:0];
    _exportPath = [documentsDirectoryPath stringByAppendingPathComponent:aFileName];  // may need to hang onto him

    if ([[NSFileManager defaultManager] fileExistsAtPath:_exportPath]) {
        [[NSFileManager defaultManager] removeItemAtPath:_exportPath
                                                   error:nil];
    }

Upvotes: 0

ader
ader

Reputation: 5393

firstly write the file atomically, then try:

self.receiptImage1 = [UIImage imageWithContentsOfFile:fullPathToFile];
self.receiptImageView1 = [UIImageView initWithImage:self.receiptImage1];

Upvotes: 0

Deviator
Deviator

Reputation: 688

Use this:

- (void)saveImage:(UIImage*)image:(NSString*)imageName{
NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.

NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it

NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory

NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path 

[fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)

NSLog(@"image saved");}

Upvotes: 6

Related Questions