简体   繁体   中英

UIImageView does not show Image from UIImagePickerController

I am trying to take a picture with my app and then show it in a UIImageView that is predefined in my .xib. I have a IBOutlet UIImageView *_foto that is linked to the UIImageView in the .xib

When I do the following the picture doesnt show in the UIImageView after taking a picture:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
    {

        _foto.image = image;
        //Also tried set image, and resizing the image first

        [self dismissModalViewControllerAnimated:YES];

    }

Now, when I add code to create a new image view with the image returned from my picker, and add that as a subView to my view like this, something strange happens:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{


    UIImageView *iv = [ [UIImageView alloc] initWithImage:image];

    [self.view addSubview:iv];

    [iv release];

    _foto.image = image;

    [self dismissModalViewControllerAnimated:YES];
}

Now the image shows in both the newly created image view in the left top corner of my view,as expected, but also in my _foto UIImageView. This has me seriously confused, and I have no idea what is going on, all i know is that it is not the behavior i expected. Does anyone have any clue, and hopefully a proper solution for this problem?

Artur Ozierański's answer is partially correct.

If you're using viewDidLayoutSubviews() to configure your layout, this gets called when the picker disappears, so if you're setting the default image in viewDidLayoutSubviews(), you'll reset back to the default image.

Configure the default image in viewDidLoad.

UIImagePickerController grab a lot of device memory while picking an image. During - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo I've noticed many times memory warnings level 1 and 2. It may cause force unload and reload your view, launch viewDidLoad method of your view controller again. If your _foto object is an IBOutlet it will be removed from memory and load again with start values of all properties (image too). Also if you set image to nil in viewDidLoad it may be a reason.

Put some NSLog into viewDidLoad to check out if it is relaunched. Also you may try to put captured image into library - if image exists in library the problem probably is in view unloading.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM