Save photo taken by UIImagePickerController

  • Hi there,

    I've in my code on method for getting image from UIImagePickerController and another one for taking Picture but i have a problem. It seems that my app don't know what method he has to call.
    I don't know how to explain so here's the code :

    //L'utilisateur a selectionner une image.
    -(void)imagePickerController:(UIImagePickerController *)imgPicker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
    {
        //On fait disparaitre la vue.
        [self dismissModalViewControllerAnimated:YES];
        NSLog(@"Sauvegarde de l'image"); //Message de log

        //On récupère les informations concernants l'image
        NSData* imageData = UIImagePNGRepresentation(image);
        NSString* imageName = @"tempImage.jpg"; //On lui donne un nom

        //On récupère le path de l'application
        NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString* documentsDirectory = [paths objectAtIndex:0]; //On le mets au debut

        //On recupere le chemin entier du fichier
        fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
        [imageData writeToFile:fullPathToFile atomically:NO];
        NSLog(@"Photo enregistrée avec succes");

    }
    As you can see, i'm french :) This code works perfectly, I can access to my PhotoLibrary and save the picture in my app but here's the problem :
    - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {

        UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

    }

    - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
    {
        UIAlertView *alert;

        if (error)
            alert = [[UIAlertView alloc] initWithTitle:@"Erreurr"
                                              message:@"Impossible de sauvegarder l'image dans l'album photo"
                                              delegate:self cancelButtonTitle:@"Ok"
                                    otherButtonTitles:nil];
        else
            alert = [[UIAlertView alloc] initWithTitle:@"Succes"
                                              message:@"L'image a bien ete enregistre."
                                              delegate:self cancelButtonTitle:@"Ok"
                                    otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    The method didFinishPickingMediaWithInfo is no longer call in my app and i don't know why. I see that code on stackoverflow and the doc on Xcode doesn't seems to be helpful.

    Do you have an idea ?

    Thanks in advance for your answer.

    Regards.

    Vavelin Kévin
    Twitter | Blog | LinkedIn
    Entrepreneur
    Developer OS X / iOS
  • On 11 Jul 2012, at 7:18 AM, Vavelin Kevin wrote:

    > As you can see, i'm french :) This code works perfectly, I can access to my PhotoLibrary and save the picture in my app but here's the problem :

    > - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    > {
    >
    > UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    >
    > UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    >
    > }

    Have you verified that image is not nil? Framework string constants are not guaranteed (very often do not) to have the same content as their names. The value of the global NSString constant UIImagePickerControllerOriginalImage may not be @"UIImagePickerControllerOriginalImage".

    > The method didFinishPickingMediaWithInfo is no longer call in my app and i don't know why. I see that code on stackoverflow and the doc on Xcode doesn't seems to be helpful.

    By "no longer" called, I assume you mean that it had been called, but the calls stopped coming. Possibly passing nil to UIImageWriteToSavedPhotosAlbum() (because the objectForKey: returned nil from an unused key) broke the framework in some way that stopped the callbacks.

    I don't know (I honestly don't know) whether the callback from UIImageWriteToSavedPhotosAlbum() is called on the main thread. The documentation says only that the callback will arrive "asynchronously." Add

    assert([NSThread isMainThread]);

    to the beginning of the callback method. If the assertion triggers, then your use of UIAlertView will damage your app (it ought to crash, but sometimes one is not so lucky).

    I don't know if these are the cause of your problem. I look forward to someone's pointing out some API error that I missed in my reading of your code.

    — F

    --
    Fritz Anderson -- Xcode 4 Unleashed: Now in stores! -- <http://x4u.manoverboard.org/>
  • Hi Fritz,

    > By "no longer" called, I assume you mean that it had been called, but the calls stopped coming.

    No i mean the function is never called. I've found something who can work perfectly but i need to reopen my PhotoLibrary for using the photo who was just taken.

    I'm waiting for this action : when the user tap on "Use", my photo is used. But I don't know what method I have to call :/
    I can not explain exactly what i mean but i'm sure you can understand with my picture ;)

    Thanks for your help.

    Vavelin Kévin
    Twitter | Blog | LinkedIn
    Entrepreneur
    Developer OS X / iOS
  • Hi Fritz,

    > By "no longer" called, I assume you mean that it had been called, but the calls stopped coming.

    No i mean the function is never called. I've found something who can work perfectly but i need to reopen my PhotoLibrary for using the photo who was just taken.

    I'm waiting for this action : when the user tap on "Use", my photo is used. But I don't know what method I have to call :/

    Thanks for your help.

    Vavelin Kévin
    Twitter | Blog | LinkedIn
    Entrepreneur
    Developer OS X / iOS
  • On Jul 11, 2012, at 5:18 AM, Vavelin Kevin <vavelinkevin...> wrote:

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

    This may not be a problem (due to particular conventions we tend to keep) but why are you using a constant string here instead of the actual constant provided by the framework? There is no guarantee that the value of UIImagePickerControllerOriginalImage is the same as @"UIImagePickerControllerOriginalImage", and if that happens to be the case here then your image will always be nil.

    >
    > UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    >
    > }

    --
    David Duncan
previous month july 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today