0
votes

I'm using a simple implementation of UIImagePickerController to take a picture and save it to my app directory. I"m running into problems with didFinishPickingMediaWithInfo, where I retrieve the image from the info, convert it into NSData using UIImageJPEGRepresentation, and then save it to a file. Problem is, the NSData object appears never to be overwritten, so the first time I take a picture it saves the picture just fine, but any subsequent pictures are not saved, because when I redisplay the image located at the filePath I'm saving it to, it's always the very first picture I took.

I have NSLog statements right now printing out the hash of the UIImage and of the NSData object I'm saving the image to. The UIImage hash changes each time, but the NSData hash is exactly the same all the time...shouldn't that change too?

My code is below. Note that everything not declared in the delegate method below is a global var that isn't causing me any trouble (at least they don't seem to be).

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    documentsDirectory = [paths objectAtIndex:0];          
    filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileNameString];

    NSData *imageData = UIImageJPEGRepresentation(image, 0.5f);
    //    imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.5f)];

    [imageData writeToFile:filePath atomically:YES];

    [self dismissModalViewControllerAnimated:YES];
}

When I get rid of the NSData object entirely and replace all instances of

imageData

with

UIImageJPEGRepresentation(image, 0.5f)

I still have the same problem, namely that the hash of UIImageJPEGRepresentation(image, 0.5f) is always the same, even though the hash of the UIImage it takes as a parameter always changes.

1

1 Answers

0
votes

Try this:

 NSData *imageData = UIImageJPEGRepresentation(image, 0.5f);
 imageData = [NSKeyedArchiver archivedDataWithRootObject:imageData];
//imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.5f)];

NSError *error= nil;
[imageData writeToFile:filePath options:NSDataWritingAtomic error:&error];

Also take a look at this link

Are you creating the directory? Use this code to create the directory:

NSArray* dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                        NSUserDomainMask, YES);
NSString* docsDir = [dirPaths objectAtIndex:0];

pathString = [docsDir stringByAppendingPathComponent:@"%@",filePath];

NSFileManager* filemgr = [NSFileManager defaultManager];

//Creates  Directory
if ([filemgr createDirectoryAtPath:pathString
       withIntermediateDirectories:YES 
                        attributes:nil 
                             error: NULL] == YES){