【问题标题】:Image is not loading in view after selection from library with xcode 5使用 xcode 5 从库中选择后,图像未加载到视图中
【发布时间】:2014-04-26 19:14:42
【问题描述】:

我正在尝试从 iphone 的库中简单地选择一张图像并将其显示在视图上。这里是头文件参考:

@interface myappViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>{
UIImagePickerController *imagepicker;
UIImagePickerController *imagepicker2;
UIImage *image;
IBOutlet UIImageView *imagepickerview;

}

- (IBAction)TakePhoto;
- (IBAction)ChooseExisting;

@end

下面是实现代码:

@interface myappViewController ()

@end

@implementation myappViewController

- (IBAction)TakePhoto{

imagepicker = [[UIImagePickerController alloc] init];
imagepicker.delegate = self;
[imagepicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:imagepicker animated:YES completion:NULL];

}

- (IBAction)ChooseExisting{

imagepicker2 = [[UIImagePickerController alloc] init];
imagepicker2.delegate = self;
[imagepicker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagepicker2 animated:YES completion:NULL];

}

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

image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imagepickerview setImage:image];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)imagepickerview{

[self dismissViewControllerAnimated:YES completion:NULL];

}

在故事板上我有两个按钮。一种用于选择现有的,一种用于拍照。如果我选择选择现有的库,我可以选择一个图像。问题是图像没有被选中,然后被重定向回 xcode 视图。相反,我必须单击照片库导航控制器中的取消按钮,然后才能看到视图中加载的图像。知道为什么加载不正确吗?

【问题讨论】:

    标签: ios objective-c view uiimagepickercontroller


    【解决方案1】:

    请使用valueForKey 而不是objectForKey 并在获取图像之前关闭图像选择器。

    [self dismissViewControllerAnimated:NO completion:nil];
    
    image = [info valueForKey:UIImagePickerControllerOriginalImage];
    [imagepickerview setImage:image];
    

    【讨论】:

    • 谢谢,已经解决了。我确实收到了两个警告。 1) dismissModalViewControllerAnimated is deprecated2) Local declaration of imagepicker hides instance variable 清除这些问题的最佳方法是什么?
    • 使用“[self dismissViewControllerAnimated:NO completion:nil];”删除所有警告。答案已更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多