如何从 iOS 照片库中获取图像并将其显示在 UIWebView 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20595227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:46:44  来源:igfitidea点击:

How do I get an image from the iOS photo library and display it in UIWebView

iosobjective-cuiwebview

提问by X0r0N

I've seen a lot of examples where you use the UIImagefor outputting an image. I would like the output to be set to a UIWebViewbecause I want to put some additional HTML formatting around the image.

我见过很多使用UIImage输出图像的例子。我希望将输出设置为 a,UIWebView因为我想在图像周围放置一些额外的 HTML 格式。

I want to get the photo library to return the relative path of an image stored on the iOS device, so that the I can put that in the 'src' attribute of the 'img' HTML tag.

我想让照片库返回存储在 iOS 设备上的图像的相对路径,以便我可以将它放在 'img' HTML 标签的 'src' 属性中。

Is this possible?

这可能吗?



Edit 1

编辑 1

I had to modify my code for what I wanted but this doesn't seem to work.

我不得不修改我想要的代码,但这似乎不起作用。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //Get Image URL from Library
    NSURL *urlPath = [info valueForKey:UIImagePickerControllerReferenceURL];
    NSString *urlString = [urlPath absoluteString];
    NSLog(urlString);

    NSURL *root = [[NSBundle mainBundle] bundleURL];
    NSString *html;
    html = @"<img src='";
    html = [html stringByAppendingString:urlString];
    html = [html stringByAppendingString:@"' />"];
    [MemeCanvas loadHTMLString:html baseURL:root];

    [picker dismissViewControllerAnimated:YES completion:^{}];
}

I am able to log the asset-library URL but it doesn't seem to be displayed the image on the UIWebView. I don't know what I need to change the baseURL to.

我能够记录资产库 URL,但它似乎没有在UIWebView. 我不知道我需要将 baseURL 更改为什么。

Any help appreciated.

任何帮助表示赞赏。



Edit 2

编辑 2

I changed UIImagePickerControllerReferenceURLto UIImagePickerControllerMediaURLand now it crashes because it doesnt like it when i append it to a string with stringByAppendingString:urlString

我换UIImagePickerControllerReferenceURLUIImagePickerControllerMediaURL现在它崩溃,因为它不喜欢,当我把它添加到一个字符串stringByAppendingString:urlString

It gives me the error:

它给了我错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSCFConstantString stringByAppendingString:]: nil argument'

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“*** -[__NSCFConstantString stringByAppendingString:]:nil 参数”

Do you know what could be causing this?

你知道是什么原因造成的吗?

回答by Lirik

[Updated to swift 4] You can use UIImagePickerControllerdelegate to select the image (or url) you want to edit.

[更新到 swift 4] 您可以使用UIImagePickerController委托来选择要编辑的图像(或 url)。

you can do this like this:

你可以这样做:

let pickerController = UIImagePickerController()
pickerController.sourceType = .photoLibrary
pickerController.delegate = self
self.navigationController?.present(pickerController, animated: true, completion: {
})

in the delegate implementation you can use the path of the image or the image itself:

在委托实现中,您可以使用图像的路径或图像本身:

// This method is called when an image has been chosen from the library or taken from the camera.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {    
   //You can retrieve the actual UIImage
   let image = info[UIImagePickerControllerOriginalImage] as? UIImage
   //Or you can get the image url from AssetsLibrary
   let path = info[UIImagePickerControllerReferenceURL] as? URL
   picker.dismiss(animated: true, completion: nil)
}

回答by Zaid Pathan

Swift 4

斯威夫特 4

class ViewController: UIViewController {
    var pickedImage:UIImage?

    @IBAction func ActionChooseImage(sender:UIButton){
        let imagePickerController = UIImagePickerController()
        imagePickerController.sourceType = .photoLibrary
        imagePickerController.delegate = self
        self.present(imagePickerController, animated: true, completion: nil)
    }
}

extension ViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let image = info[UIImagePickerControllerOriginalImage] as? UIImage
        self.pickedImage = image

        picker.dismiss(animated: true, completion: nil)
    }
}

Ref

参考

回答by Chris Byatt

Updated for Swift 3.0

为 Swift 3.0 更新

let imagePickerController = UIImagePickerController()
imagePickerController.sourceType = .PhotoLibrary
imagePickerController.delegate = self
self.presentViewController(imagePickerController, animated: true, completion: nil)

And the delegate:

和代表:

extension YourViewController: UIImagePickerControllerDelegate {
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        // Whatever you want here
        self.pickedImage = image
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
}

回答by Vaisakh

You can load Image from Photolibrary into webview as shown below code

您可以将 Photolibrary 中的图像加载到 webview 中,如下代码所示

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info    
       {
      NSURL *urlPath = [info valueForKey:UIImagePickerControllerReferenceURL];
      UIImage *cameraImage = [info valueForKey:UIImagePickerControllerOriginalImage];
      NSData *myData = UIImagePNGRepresentation(cameraImage);
      [self.webview loadData:myData MIMEType:@"image/png" textEncodingName:nil baseURL:nil];
      [self.picker dismissViewControllerAnimated:YES completion:nil];
       }

回答by Balram Tiwari

Please note that you have to conform to protocol UIImagePickerControllerDelegate

请注意,您必须遵守协议 UIImagePickerControllerDelegate

UIImagePickerController *myImagePicker = [[UIImagePickerController alloc] init];
        myImagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        myImagePicker.delegate = self;
        [self presentViewController:myImagePicker animated:YES completion:nil];

Load an Image & then Show in WebView.

加载图像,然后在 WebView 中显示。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //Get Image URL from Library
    NSURL *urlPath = [info valueForKey:UIImagePickerControllerReferenceURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:urlPath];
    [webview loadRequest:requestObj];
}