如何从 iOS 照片库中获取图像并在 UIWebView 中显示
Posted
技术标签:
【中文标题】如何从 iOS 照片库中获取图像并在 UIWebView 中显示【英文标题】:How do I get an image from the iOS photo library and display it in UIWebView 【发布时间】:2013-12-15 13:47:21 【问题描述】:我见过很多使用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
的字符串时它不喜欢它
它给了我错误:
由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'*** -[__NSCFConstantString stringByAppendingString:]: 无参数'
你知道是什么原因造成的吗?
【问题讨论】:
如果您只是在图像周围布局一些字符串内容,如何使用 TextKit。感谢@rokjarc 分享了一个好问题。 您需要文件 URL,而不是资产库 URL。将UIImagePickerControllerReferenceURL
更改为UIImagePickerControllerMediaURL
。
【参考方案1】:
[更新到 swift 4]
您可以使用UIImagePickerController
delegate 选择您要编辑的图片(或网址)。
你可以这样做:
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)
【讨论】:
如果我使用相机拍摄照片然后[info valueForKey:UIImagePickerControllerReferenceURL];
给我(空)价值.. 任何解决方案?【参考方案2】:
斯威夫特 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
【讨论】:
【参考方案3】:为 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)
【讨论】:
【参考方案4】:您可以将图片从 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];
【讨论】:
【参考方案5】:请注意,您必须遵守协议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];
【讨论】:
这仍然提供资产库 URL,并且您正在删除 OP 想要的所有 HTML。以上是关于如何从 iOS 照片库中获取图像并在 UIWebView 中显示的主要内容,如果未能解决你的问题,请参考以下文章
iPhone:如何使用 iPhone 照片应用程序等“编辑”功能从照片库或相机中获取图像