【问题标题】:iPhone SDK - How to display a photo taken with the camera inside a UINavigationController?iPhone SDK - 如何在 UINavigationController 中显示用相机拍摄的照片?
【发布时间】:2010-02-17 14:38:06
【问题描述】:

这是我目前的代码:

/* class: myViewController
@interface myViewController: UIViewController
       <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
*/
- (IBAction) getPicture {
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
       picker.delegate = self;
       picker.sourceType = UIImagePickerControllerSourceTypeCamera;
       [self presentModalViewController:picker animated:YES];
}

- (void) imagePickerController:(UIImagePickerController *)thePicker 
                    didFinishPickingMediaWithInfo:(NSDictionary *)imageInfo
{
    [[thePicker parentViewController] dismissModalViewControllerAnimated:YES];
       UIImage *img = [imageInfo
                           objectForKey:@"UIImagePickerControllerOriginalImage"];
    self.myImageView.image = img;
}

所以基本上我试图从 iPhone 相机中获取照片并将其显示在 UIImageView 中。只要类 myViewController 显示为独立视图,它就可以正常工作。如果我将视图放在 UINavigationController 中,则 UIImageView 在用相机拍摄后不会显示图像。但是,如果我从图书馆中选择一张图片,一切都会好起来的。

那么为什么 UIImageView 不会在 UINavigationController 中显示用相机拍摄的图像呢?

【问题讨论】:

    标签: iphone uinavigationcontroller uiimageview camera


    【解决方案1】:

    尝试切换以下几行:

    [thePicker dismissModalViewControllerAnimated:YES];
    UIImage *img = [imageInfo objectForKey:@"UIImagePickerControllerOriginalImage"];
    self.myImageView.image = img;
    

    到以下:

    UIImage *img = [imageInfo objectForKey:@"UIImagePickerControllerOriginalImage"];
    self.myImageView.image = img;
    [thePicker dismissModalViewControllerAnimated:YES];
    

    也许图像被释放是因为在你保留它之前关闭了视图

    self.myImageView.image = img;
    

    【讨论】:

    • 设置图片后有没有尝试过以下操作? [self setNeedsDisplay];
    • 不幸的是它并没有改变任何东西。
    【解决方案2】:

    在我看来,您解散了错误的视图控制器。不应该:

    [thePicker dismissModalViewControllerAnimated:YES];
    

    阅读:

    [self dismissModalViewControllerAnimated:YES];
    

    【讨论】:

    • 我刚刚尝试过,但没有任何区别。你能再给我解释一下吗? thePicker 应该是当前的 ImagePicker。
    • thePicker 是当前的 ImagePicker,但我认为您应该将 dismissModalViewControllerAnimated 调用发送到创建模态视图控制器而不是模态视图控制器本身的视图控制器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    相关资源
    最近更新 更多