如何从iOS照片库中获取图像并将其显示在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 = @""]; [MemeCanvas loadHTMLString:html baseURL:root]; [picker dismissViewControllerAnimated:YES completion:^{}]; }
我能够记录资产库URL,但似乎没有在UIWebView
上显示图像。 我不知道我需要将baseURL更改为。
任何帮助赞赏。
编辑2
我将UIImagePickerControllerReferenceURL
更改为UIImagePickerControllerMediaURL
,现在它崩溃了,因为当我将它附加到stringByAppendingString:urlString
的字符串时它不喜欢它stringByAppendingString:urlString
它给了我错误:
因未捕获的exception’NSInvalidArgumentException’而终止应用程序,原因:’*** – [__ NSCFConstantString stringByAppendingString:]:nil参数’
你知道是什么原因引起的吗?
您可以使用UIImagePickerController委托来选择要编辑的图像(或URL)。
你可以这样做:
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePickerController.delegate = self; [self presentViewController:imagePickerController animated:YES completion:nil];
在委托实现中,您可以使用图像的路径或图像本身:
// 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]; }
斯威夫特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]; }