[英]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.