如何从 iOS 照片库中获取图像并在 UIWebView 中显示 [英] How do I get an image from the iOS photo library and display it in UIWebView

查看:16
本文介绍了如何从 iOS 照片库中获取图像并在 UIWebView 中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这可能吗?

<小时>

编辑 1

我不得不根据我的需要修改我的代码,但这似乎不起作用.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{//从库中获取图片地址NSURL *urlPath = [信息 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];[pickerdismissViewControllerAnimated:YES 完成:^{}];}

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

任何帮助表示赞赏.

<小时>

编辑 2

我将 UIImagePickerControllerReferenceURL 更改为 UIImagePickerControllerMediaURL,现在它崩溃了,因为当我使用 stringByAppendingString:urlString<将它附加到字符串时它不喜欢它/p>

它给了我错误:

<块引用>

由于未捕获的异常而终止应用程序'NSInvalidArgumentException',原因:'*** -[__NSCFConstantStringstringByAppendingString:]: 无参数'

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

解决方案

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

你可以这样做:

让pickerController = UIImagePickerController()pickerController.sourceType = .photoLibrarypickerController.delegate = selfself.navigationController?.present(pickerController,动画:真,完成:{})

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

//当从库中选择图像或从相机拍摄图像时调用此方法.func imagePickerController(_picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {//你可以检索实际的UIImage让 image = info[UIImagePickerControllerOriginalImage] 作为?UIImage//或者你可以从AssetsLibrary中获取图片的url让 path = info[UIImagePickerControllerReferenceURL] 作为?网址picker.dismiss(动画:真,完成:无)}

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

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.

Is this possible?


Edit 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.

Any help appreciated.


Edit 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

It gives me the error:

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

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.

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

这篇关于如何从 iOS 照片库中获取图像并在 UIWebView 中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆