繁体   English   中英

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

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

我已经看过很多使用UIImage输出图像的例子。 我希望将输出设置为UIWebView因为我想在图像周围添加一些额外的HTML格式。

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

这可能吗?


编辑1

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

- (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:^{}];
}

我能够记录资产库URL,但似乎没有在UIWebView上显示图像。 我不知道我需要将baseURL更改为。

任何帮助赞赏。


编辑2

我将UIImagePickerControllerReferenceURL更改为UIImagePickerControllerMediaURL ,现在它崩溃,因为当我将它附加到stringByAppendingString:urlString的字符串时它不喜欢它stringByAppendingString:urlString

它给了我错误:

因未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'*** - [__ NSCFConstantString stringByAppendingString:]:nil参数'

你知道这可能导致什么吗?

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

你可以这样做:

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

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

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

斯威夫特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)
    }
}

参考

更新了Swift 3.0

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

代表:

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

您可以将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];
       }

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

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

加载图像然后在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