load local image into UIWebView

依然范特西╮ 提交于 2019-12-04 19:12:16

Convert it to a base64 data URI scheme and send that via Javascript as you're currently doing :

someDOMelement.src = 'url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO 9TXL0Y4OHwAAAABJRU5ErkJggg==)';

If you don't need to store the image, you can convert it to NSData and load the UIWebView directly, skipping the URL altogether:

NSData *data = UIImagePNGRepresentation(viewImage);
[myUIView loadData:data MIMEType:@"image/jpeg" textEncodingName:@"UTF-8" baseURL:nil];

UPDATED:

Since you do need to store the file, use NSFileManager to get the URL of your directory, then use the URL to write to and retrieve the file:

NSURL *pathUrl = [[NSFileManager defaultManager] URLForDirectory:directory inDomains:NSDocumentDirectory appropriateForURL:nil create:NO error:nil];
NSURL *fileUrl = [pathUrl URLByAppendingPathComponent:file];
[UIImagePNGRepresentation(viewImage) writeToURL:fileUrl atomically:YES];
NSString *urlString = [fileUrl absoluteString];

You haven't saved the image to that path.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!