简体   繁体   English

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

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

I've seen a lot of examples where you use the UIImage for outputting an image. 我已经看过很多使用UIImage输出图像的例子。 I would like the output to be set to a UIWebView because I want to put some additional HTML formatting around the image. 我希望将输出设置为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 . 我能够记录资产库URL,但似乎没有在UIWebView上显示图像。 I don't know what I need to change the baseURL to. 我不知道我需要将baseURL更改为。

Any help appreciated. 任何帮助赞赏。


Edit 2 编辑2

I changed UIImagePickerControllerReferenceURL to UIImagePickerControllerMediaURL and now it crashes because it doesnt like it when i append it to a string with stringByAppendingString:urlString 我将UIImagePickerControllerReferenceURL更改为UIImagePickerControllerMediaURL ,现在它崩溃,因为当我将它附加到stringByAppendingString:urlString的字符串时它不喜欢它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? 你知道这可能导致什么吗?

[ Updated to swift 4 ] You can use UIImagePickerController delegate 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)
}

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 参考

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)
    }
}

You can load Image from Photolibrary into webview as shown below code 您可以将Image from 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];
       }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM