UIImagePicker不显示所选图像

时间:2009-12-04 10:06:28

标签: iphone uiimagepickercontroller

我是iPhone新手,我正在使用此代码从iPhone库中选择图像并在imageView中显示。该库正在显示图像,但是当我选择图像时,它不会出现在图像视图中..

- (void)viewDidLoad {
    [super viewDidLoad];

 UIImagePickerController * picker = [[UIImagePickerController alloc] init];

 picker.delegate = self;

 picker.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;

 [self presentModalViewController:picker animated:YES];
}

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

 [picker dismissModalViewControllerAnimated:YES];

 imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}

请告诉我代码有什么问题?

2 个答案:

答案 0 :(得分:0)

尝试:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    imageView.image = pickedImage;

    [self dismissModalViewControllerAnimated:YES];
}

UIImagePickerControllerOriginalImage是一个字符串常量,可以与@“UIImagePickerControllerOriginalImage”字符串不同。 然后,前者设置图像视图是安全且更清晰的,后者则关闭模态视图控制器。

注意你应该调用dismissModalViewControllerAnimated:on self,因为你正在解雇自己“拥有”的模态视图控制器。

再见!

答案 1 :(得分:0)

我修复了“灰色框出现而不是UIImageView中的图像”的问题。

我忘了将UIIMageView的背景颜色设置为CLEAR COLOR。通过将背景颜色设置为clearColor来解决问题。 使用XIB时,如果使用界面构建器创建UI,还应检查“界面”构建器中是否有任何错误。