简体   繁体   English

UIImageView不显示来自UIImagePickerController的图像

[英]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. 我试图用我的应用拍照,然后在.xib中预定义的UIImageView中显示它。 I have a IBOutlet UIImageView *_foto that is linked to the UIImageView in the .xib 我有一个IBOutlet UIImageView * _foto,它链接到.xib中的UIImageView

When I do the following the picture doesnt show in the UIImageView after taking a picture: 当我执行以下操作时,拍照后图片不会显示在UIImageView中:

- (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. 现在,图像既显示在视图左上角的新创建的图像视图中,也可以显示在我的_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. ArturOzierański的回答部分正确。

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. 如果您使用viewDidLayoutSubviews()配置布局,则在选择器消失时会调用此方法,因此,如果您在viewDidLayoutSubviews()中设置默认图像,则将重置为默认图像。

Configure the default image in viewDidLoad. 在viewDidLoad中配置默认​​图像。

UIImagePickerController grab a lot of device memory while picking an image. UIImagePickerController在拾取图像时会占用大量设备内存。 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. - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo我注意到内存警告级别为1和2多次。它可能会导致强制卸载并重新加载视图,启动再次使用视图控制器的viewDidLoad方法。 If your _foto object is an IBOutlet it will be removed from memory and load again with start values of all properties (image too). 如果您的_foto对象是IBOutlet,它将从内存中删除,并使用所有属性的起始值(也包括图像)再次加载。 Also if you set image to nil in viewDidLoad it may be a reason. 另外,如果您在viewDidLoad中将图片设置为nil,则可能是原因。

Put some NSLog into viewDidLoad to check out if it is relaunched. 将一些NSLog放入viewDidLoad中,以检查是否重新启动。 Also you may try to put captured image into library - if image exists in library the problem probably is in view unloading. 您也可以尝试将捕获的图像放入库中-如果库中存在图像,则问题可能出在视图卸载中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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