UIImagePicker 不显示所选图像

发布于 2024-08-13 16:00:00 字数 674 浏览 1 评论 0原文

我是 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"];
}

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

I'm new to iPhone and I'm using this code to select an image from the iPhone library and show it in imageView. The library is showing images, but when I'm selecting the image, it doesn't appear in the image view..

- (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"];
}

Please can you tell me what's the problem with the code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

享受孤独 2024-08-20 16:00:00

尝试使用:

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

    [self dismissModalViewControllerAnimated:YES];
}

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

请注意,您应该在 self 上调用 dismissModalViewControllerAnimated: 因为您正在解除 self“拥有”的模态视图控制器。

再见!

Try with:

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

    [self dismissModalViewControllerAnimated:YES];
}

UIImagePickerControllerOriginalImage is a string constant that could be different from @"UIImagePickerControllerOriginalImage" string.
Then, it's safe and more clear to former set the image view, and latter to dismiss the modal view controller.

Pay attention that you should invoke dismissModalViewControllerAnimated: on self because you are dismissing the modal view controller "owned" by self.

Bye!

一笔一画续写前缘 2024-08-20 16:00:00

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

我忘记将 UIIMageView 的背景颜色设为透明颜色。通过将背景颜色设置为clearColor 解决了该问题。
正在使用 XIB,如果您使用界面生成器创建 UI,您还应该检查界面生成器中是否有任何布局错误。

I fixed my issue of "grey box appearing instead of an image in UIImageView".

I forgot to make the Background color of the UIIMageView to be CLEAR COLOR. THe problem was solved by setting background color to clearColor.
Was using XIB and you should also check if anything is laid out wrong in Interface builder in case you are using interface builder to create UI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文