[英]Display UIImage from camera in UIWebView
In my iPhone app, I want the user to be able to take a picture with the camera, then have that image appear in amongst some locally stored HTML in a UIWebView
. 在我的iPhone应用程序中,我希望用户能够使用相机拍照,然后将该图像显示在UIWebView
中的一些本地存储的HTML中。
So I've got the UIImage
object from the UIImagePickerController
, now I need to find out how to save it to memory so I can reference it in the UIWebView
. 所以我从UIImagePickerController
获得了UIImage
对象,现在我需要找出如何将它保存到内存中,以便我可以在UIWebView
引用它。
Note that I don't just want the image on it's own in the UIWebView
(I want to use the picture as a background-image in some HTML with other images layered on top). 请注意,我不只是想在UIWebView
拥有它自己的图像(我希望将图片用作某些HTML中的背景图像,其他图像分层在顶部)。 I'm also using other images and HTML that are stored in the app, that I'm referencing by setting the baseURL
of the UIWebView to the bundle path, like: 我还使用存储在应用程序中的其他图像和HTML,我通过将UIWebView的baseURL
设置为包路径来引用,例如:
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[self.webView loadHTMLString:html baseURL:baseURL];
You definitely have the option of saving the file locally, getting it's absolute path and using that. 你绝对可以选择在本地保存文件,获取它的绝对路径并使用它。 Another option I used for a hybrid app once is converting the UIImage to Base64 string and pass that through javascript to the webview to do whatever you want it to do. 我用于混合应用程序的另一个选项是将UIImage转换为Base64字符串并将其通过javascript传递给webview以执行您想要的任何操作。 To do that that after getting the UIImage encode it (there are various libraries out there to do this: 为了做到这一点,在获得UIImage编码之后(有各种各样的库来做到这一点:
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSString *base64EncodedImage = [Base64 encode:UIImageJPEGRepresentation(image, 0.5)];
Then in your HTML you could have method that is given the base64 images and sets it to some IMG or background or whatever you need: 然后在您的HTML中,您可以使用给定base64图像的方法并将其设置为某些IMG或背景或您需要的任何内容:
function cameraCallback(imageData) {
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + imageData;
}
Or 要么
<img src="data:image/gif;base64, [YOUR BASE64 STRING HERE]" alt="" width="80" height="15" />
Then in the HTML in the webview you would use 然后在webview中的HTML中使用
[mywebview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"cameraCallback(%@)", base64EncodedImage]];
This is the way I ended up doing this. 这就是我最终做到这一点的方式。 In the code that handles the image taken using the camera I actually save the file directly to disc: 在处理使用相机拍摄的图像的代码中,我实际上将文件直接保存到光盘:
// Get the image out of the camera
UIImage *image = (UIImage *)[info valueForKey:UIImagePickerControllerOriginalImage];
// Images from the camera are always in landscape, so rotate
UIImage *rotatedImage = scaleAndRotateImage(self.image);
// Save the image to the filesystem
NSData *imageData = UIImagePNGRepresentation(rotatedImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString* savePath = [documentsPath stringByAppendingPathComponent:@"CameraPhoto.png"];
BOOL result = [imageData writeToFile:savePath atomically:YES];
The function to rotate the image is copied straight from this blog, http://blog.logichigh.com/2008/06/05/uiimage-fix/ . 直接从此博客http://blog.logichigh.com/2008/06/05/uiimage-fix/复制旋转图像的功能。
Then when I want to display this image within the UIWebView, I just do a string replace to reference inject the path to the image. 然后当我想在UIWebView中显示这个图像时,我只是做一个字符串替换来引用注入图像的路径。 So in my HTML there's like <img src="{CameraPhotoUrl}" />
and then: 所以在我的HTML中,有像<img src="{CameraPhotoUrl}" />
然后:
// Build the reference to the image we just saved
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *cameraImagePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"CameraPhoto.png?x=%@", [[NSDate date] description]]];
// Load the HTML into the webview
NSString *htmlFilePath = [[NSBundle mainBundle] pathForResource:@"MyHtmlFile" ofType:@"htm"];
NSString *html = [NSString stringWithContentsOfFile:htmlFilePath encoding:NSUTF8StringEncoding error:nil];
html = [html stringByReplacingOccurrencesOfString:@"{CameraPhotoUrl}" withString:cameraImagePath];
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[self.webView loadHTMLString:html baseURL:baseURL];
Voila! 瞧! Note that I'm appending a date-based query string parameter to the CameraPhoto.png
filename simply to prevent caching. 请注意,我将基于日期的查询字符串参数附加到CameraPhoto.png
文件名,以防止缓存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.