UIImagePickerController 很容易使用,但是当我以前没有发现它时,我突然发现它令人恼火。发生的事情是,有时imagePickerController:didFinishPickingImage:editingInfo 委托方法似乎不起作用——即使在分配完成后,图像也不会显示在 UIImageView 中。有时会,有时不会,而且,我尝试过的每一段示例代码(来自网络,来自“Beginning iPhone 3 Development”一书等)都会出现同样的问题。我不知道为什么,这个问题发生在我的 iPhone 3G 和 3GS 上,所以我怀疑这是硬件问题。这些设备运行 OS 3.1.2。视图控制器是从包含一个按钮和 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