2

Trying to select image using photo picker and save that image internally in apps folder.

- (void) imagePickerController: (UIImagePickerController *) pickerdidFinishPickingMediaWithInfo: (NSDictionary *) info {

NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToUse;

// Handle a still image picked from a photo album
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
    == kCFCompareEqualTo) {

    editedImage = (UIImage *) [info objectForKey:
                               UIImagePickerControllerEditedImage];
    originalImage = (UIImage *) [info objectForKey:
                                 UIImagePickerControllerOriginalImage];

    if (editedImage) {
        imageToUse = editedImage;
    } else {
        imageToUse = originalImage;
    }
    // Do something with imageToUse

    //save it
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *imageName = [documentsDirectory stringByAppendingString:[NSString stringWithFormat:@"%d", myUniqueID]];
    NSString *imagePath = [imageName stringByAppendingPathComponent:@".png"];

    NSData *webData = UIImagePNGRepresentation(editedImage);
    NSError* error = nil;
    bool success = [webData writeToFile:imagePath options:NULL error:&error];
    if (success) {
        // successfull save
        imageCount++;
        [[NSUserDefaults standardUserDefaults] setInteger:imageCount forKey:@"imageCount"];
        NSLog(@"@Success save to: %@", imagePath);
    }
    else if (error) {
        NSLog(@"Error:%@", error.localizedDescription);
    }
}
...
}

What I can't figure out is that writeToFile::: returns false but no value is returned in error so I can't figure out whats going wrong. Any help would be greatly appreciated thanks

Z.O.
  • 395
  • 1
  • 4
  • 12

1 Answers1

1

You're missing a "/". The line:

NSString *imageName = [documentsDirectory stringByAppendingString:[NSString stringWithFormat:@"%d", myUniqueID]];

should be:

NSString *imageName = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d", myUniqueID]];

And the line that says:

NSString *imagePath = [imageName stringByAppendingPathComponent:@".png"];

should be:

NSString *imagePath = [imageName stringByAppendingPathExtension:@"png"];

Update:

And, shouldn't:

NSData *webData = UIImagePNGRepresentation(editedImage);

be the following?

NSData *webData = UIImagePNGRepresentation(imageToUse);
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • thanks for the reply. Made the changes.. but still nothing. Most confused. – Z.O. Oct 07 '12 at 19:28
  • I have no idea why... but changing NSData *webData = UIImagePNGRepresentation(editedImage); to NSData *webData = [NSData dataWithData :UIImagePNGRepresentation(editedImage)]; seems to have worked – Z.O. Oct 07 '12 at 19:34
  • I don't know why it didn't work the first time, but I'd wager that if you change that `NSData` line back, it will still work... – Rob Oct 07 '12 at 19:42
  • We fixed the problem with the path. Now you need to check the results of webData (i.e. see how big it is, etc.). By the way, I notice you go through the effort of figuring out `imageToUse`, but then you just use `editedImage`. See my updated answer. – Rob Oct 07 '12 at 19:47
  • lol ya. thats embarrassing. works flawlessly now. thanks a ton @Rob !! – Z.O. Oct 07 '12 at 19:55