iphone - UIImagePickerController 的怪异

标签 iphone uiimagepickercontroller

UIImagePickerController 很容易使用,但是当我以前没有发现它时,我突然发现它很令人恼火。发生的情况是,有时 imagePickerController:didFinishPickingImage:editingInfo 委托(delegate)方法似乎不起作用 - 即使在进行分配后,图像也不会显示在 UIImageView 中。有时会,有时不会,此外,我尝试过的每一个示例代码(来自网络,来自“Beginning iPhone 3 Development”一书等)都表现出同样的问题。我不知道为什么,而且我的 iPhone 3G 和 3GS 都出现这个问题,所以我怀疑这是硬件问题。这些设备运行操作系统 3.1.2。 View Controller 从包含一个按钮和 UIImageView 的 xib 文件加载。我真的很希望有人告诉我我明显做错了什么愚蠢的事情:-)

这是代码 - 我试图制作尽可能小的应用程序来显示问题:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface imagepickerViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
    IBOutlet UIButton *button;
    IBOutlet UIImageView *imageView;    
}

@property (nonatomic, retain) UIImageView *imageView;

- (IBAction)takepic;
- (void)usePic:(UIImage *)pic;

@end




#import "imagepickerViewController.h"

@implementation imagepickerViewController

@synthesize imageView;

- (IBAction)takepic 
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;        
        picker.delegate = self;

        [self presentModalViewController:picker animated:YES];
        [picker release];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)info
{
    [self usePic:image];
    [picker dismissModalViewControllerAnimated:YES];

    // after this method returns, the UIImageView should show the image -- yet very often it does not ...
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissModalViewControllerAnimated:YES];
}

- (void)usePic:(UIImage *)picture
{
    imageView.image = picture;
}

@end

最佳答案

我最近也遇到这个问题了。我的解决方案是在使用图像之前关闭模态 UIImagePickerController View 。

通过关闭图像选择器,先前的 View Controller 的 View 将被重新加载,并且作为所选图像接收者的 UIImageView 将被重新加载并且不再为零。

即我从此改变:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    // Wrong! imageView has been released by the UIViewController after a low memory warning and is now nil.
    [imageView setImage:image];
    [self dismissModalViewControllerAnimated:YES];
}

对此:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    // Dismis the image picker first which causes the UIViewController to reload it's view (and therefore also imageView)
    [self dismissModalViewControllerAnimated:YES];
    [imageView setImage:image];
}

关于iphone - UIImagePickerController 的怪异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1671824/

相关文章:

iphone - 如何在 iphone 应用程序中将导航 Controller 的后退按钮与其左角对齐

iphone - 对象数量 (3) 不等于键数量 (8)

objective-c - 使用 Facebook iOS 签到时照片未上传

ios - 捏合缩放 UIImage

ios - 在 Swift 中选择照片时出现 "Array index out of range"

iphone - UIActionSheet 不显示——屏幕只是变暗

iPhone 和 iPad 屏幕分辨率

iphone - 2 字节数组/内联 c 数组的相等性

iphone - 如何更改 UIImagePickerController 裁剪框

ios - UIImagePickerController didFinishPickingImage 返回不正确的 imageOrientation