UIImageView does not show Image from UIImagePicker

2020-04-30 17:49发布

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?

2条回答
做自己的国王
2楼-- · 2020-04-30 18:01

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.

查看更多
Melony?
3楼-- · 2020-04-30 18:17

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.

查看更多
登录 后发表回答