如何从iOS照片库中获取图像并将其显示在UIWebView中 [英] How do I get an image from the iOS photo library and display it in UIWebView
问题描述
我见过许多使用 UIImage
输出图像的例子。我希望输出设置为 UIWebView
,因为我想在图像周围添加一些额外的HTML格式。
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.
我想让照片库返回存储在iOS设备上的图像的相对路径,这样我就可以将它放在'img'HTML标签的'src'属性中。
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.
这可能吗?
编辑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:^{}];
}
我能够记录资产库URL,但似乎没有在 UIWebView
上显示图像。我不知道我需要将baseURL更改为。
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.
任何帮助表示赞赏。
编辑2
我将 UIImagePickerControllerReferenceURL
更改为 UIImagePickerControllerMediaURL
现在它崩溃了因为它不喜欢它,当我将它附加到一个字符串 stringByAppendingString:urlString
I changed UIImagePickerControllerReferenceURL
to UIImagePickerControllerMediaURL
and now it crashes because it doesnt like it when i append it to a string with stringByAppendingString:urlString
它给了我错误:
由于未捕获的异常而终止应用
'NSInvalidArgumentException',原因:' *** - [__ NSCFConstantString
stringByAppendingString:]:nil参数'
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSCFConstantString stringByAppendingString:]: nil argument'
你知道是什么原因引起的吗? / p>
Do you know what could be causing this?
推荐答案
您可以使用UIImagePickerController委托来选择要编辑的图像(或网址)。
you can use UIImagePickerController delegate to select the image (or url) you want to edit.
你可以这样做:
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];
在委托实现中,您可以使用图像路径或图像本身:
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.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//You can retrieve the actual UIImage
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
//Or you can get the image url from AssetsLibrary
NSURL *path = [info valueForKey:UIImagePickerControllerReferenceURL];
[picker dismissViewControllerAnimated:YES completion:nil];
}
这篇关于如何从iOS照片库中获取图像并将其显示在UIWebView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!