1

I want to take a screenshot and then send to webview a local path for it, so it will show it. I know that webview could take images from NSBundle but this screenshots are created dynamically - can I add something to it by code?

I was trying it like this:

  UIGraphicsBeginImageContext( webView.bounds.size);    
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    

    NSString *file = @"myfile.png";
    NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
    NSString *path = [directory stringByAppendingPathComponent: file];
    [UIImagePNGRepresentation(viewImage) writeToFile:path atomically:YES];

    NSString *function = [NSString stringWithFormat:@"loadImagePlease(%@);",path];
    [ ad stringByEvaluatingJavaScriptFromString:function];

But loadImageFunction can't access it , with file:/// or file:// prefix. What's the solution? Thanks for ANY help.

EDIT: added a missing line in code

SOLUTION: I used a Cyrille's advice and this library for encoding : http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html

Piotr
  • 1,743
  • 1
  • 26
  • 40

3 Answers3

1

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];
highlycaffeinated
  • 19,729
  • 9
  • 60
  • 91
  • Sadly I don't want to show only the image. It's should be just small part of website and I can't load all the html&css&js code from app. It needs only a path to the local file. – Piotr Aug 05 '11 at 13:54
  • pathUrl shouldn't be like - ` NSURL *pathUrl = [ [NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSDocumentDirectory appropriateForURL:nil create:NO error:nil];` ? Anyway - it doesn't want to load it. – Piotr Aug 05 '11 at 16:50
1

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==)';

Cyrille
  • 25,014
  • 12
  • 67
  • 90
  • Hey, one more question - so Mobile Safari can't access local files itself? That's why we need such bypass? – Piotr Aug 11 '11 at 08:52
  • It can’t. At least, not until the iDevice has been jailbroken and the "file:// safari patch" has been installed. – Cyrille Aug 11 '11 at 09:19
  • What about the big images...about 5-10 mb? image data would be too long to send as parameter, what should we do than? – Harish Pathak Jun 25 '18 at 10:06
0

You haven't saved the image to that path.

Jim
  • 72,985
  • 14
  • 101
  • 108
  • I just erased it while removing comments - my fault. It's now in. Safari Mobile doesn't seem to have file:/// protocol. Where's the problem? – Piotr Aug 05 '11 at 13:52