我已经检查了几个教程,从我可以告诉我的代码是正确的,但最后我没有得到图像。我没有收到任何错误,但我试图设置的屏幕上的图像仍然是空白的。
这是我的头文件
@interface homeViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *avatarImage;
@property (weak, nonatomic) IBOutlet UIButton *photoButton;
@property (nonatomic, retain) UIImagePickerController *imgPicker;
- (IBAction)setPhoto;
@end
这是相关的方法
- (IBAction)setPhoto
{
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.sourceType=UIImagePickerControllerSourceTypeCamera;
self.imgPicker.delegate=self;
self.imgPicker.allowsEditing = YES;
self.imgPicker.showsCameraControls=YES;
[self presentModalViewController:self.imgPicker animated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
self.avatarImage = [[UIImageView alloc] init];
self.avatarImage.image = image;
[self dismissModalViewControllerAnimated:YES];
}
当我在这里放置一个断点时:[self dismissModalViewControllerAnimated:YES]; 我看到图像变量有一个值,但self.avatarImage有一个0x00000000
答案 0 :(得分:2)
由于您指定了allowsEditing = NO
,因此您需要使用UIImagePickerControllerOriginalImage
信息密钥。
注意:如果不进行编辑,可能是'EditedImage'默认为'OriginalImage'。在这种情况下,你的'image'变量将是有效的,并且self.avatarView中UIImageView的显示将是罪魁祸首。
答案 1 :(得分:1)
您使用的是错误的委托方法。使用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
您使用的委托方法在iOS 3.0中已弃用。
答案 2 :(得分:0)
行
[picker dismissModalViewControllerAnimated:YES];
[picker release];
在使用信息词典之前必须出现。