0

我检查了几个教程,据我所知我的代码是正确的,但最后我没有得到图像。我没有收到任何错误,但我尝试设置的屏幕上的图像仍然是空白的。

这是我的头文件

@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]; 我看到 image 变量有一个值,但是 self.avatarImage 有一个 0x00000000

4

3 回答 3

2

由于您指定allowsEditing = NO了您需要使用UIImagePickerControllerOriginalImageinfo 键。

Note: it may be that 'EditedImage' defaults to 'OriginalImage' if no editing occurs. In that case your 'image' variable will be valid and the display of the UIImageView in self.avatarView will be the culprit.

于 2012-06-08T19:51:34.367 回答
1

您使用了错误的委托方法。采用

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

您使用的委托方法在 iOS 3.0 中已弃用。

于 2012-06-08T19:44:46.953 回答
0

The lines

[picker dismissModalViewControllerAnimated:YES];
[picker release];

Have to appear before using the info dictionary.

于 2013-06-19T08:43:33.707 回答