[英]How to display photo library image in Xcode?
My objective-c code is erroring out with the Code=13 error, and does not display either the cameras photo or a library's image in an image view.我的objective-c 代码出错,出现Code=13 错误,并且在图像视图中不显示相机照片或库图像。
I'M Using Xcode 10.1, deployment target 12.0, and I'm going off example from UIImagePickerController not picking image in iOS 9我正在使用 Xcode 10.1,部署目标为 12.0,我将离开UIImagePickerController 的示例, 而不是在 iOS 9 中选择图像
I have two problems:我有两个问题:
1) Even though I add in "Privacy - Photo Library Usage Description | We want to use the library" in the p-list, it still gives me this error: 1)即使我在 p-list 中添加了“隐私 - 照片库使用说明 | 我们想要使用该库”,它仍然给我这个错误:
[discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled} [发现] 发现扩展时遇到的错误:Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}
What am I doing wrong?我究竟做错了什么?
2) I can't get the image to show up in the LoveImage view. 2) 我无法在 LoveImage 视图中显示图像。 What am I doing wrong here?
我在这里做错了什么?
Below is .m file:下面是 .m 文件:
-(IBAction)imagepickertapped:(id)sender {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Attach image" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* pickFromGallery = [UIAlertAction actionWithTitle:@"Take a photo"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:NULL];
}
}];
UIAlertAction* takeAPicture = [UIAlertAction actionWithTitle:@"Choose from gallery"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:NULL];
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
}];
[alertController addAction:pickFromGallery];
[alertController addAction:takeAPicture];
[alertController addAction:cancel];
[self presentViewController:alertController animated:YES completion:nil]; }
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
UIImage *LoveImage = info[UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
self.LoveImage = info[UIImagePickerControllerOriginalImage];}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];}
Change your didfinish method code to below.将您的 didfinish 方法代码更改为以下内容。
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
UIImage *LoveImage = info[UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
self.LoveImage.image = LoveImage;
}
LoveImage is an image view. LoveImage 是一个图像视图。 We can't assign an image to it directly.
我们不能直接为其分配图像。 It has an
image
property so we have to set its image
property.它有一个
image
属性,所以我们必须设置它的image
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.