cordova-screenshot icon indicating copy to clipboard operation
cordova-screenshot copied to clipboard

iOS return URI

Open tbh726 opened this issue 10 years ago • 6 comments
trafficstars

Hi, great plugin... i saw that iOS did not have URI, this was what i needed for bot android and iOS... not saying this is perfect (not an objective c type but can play), but it works great for me for URI.

in Screenshot.h

  • (void)getScreenshotAsURI:(CDVInvokedUrlCommand*)command;

in Screenshot.m

  • (void)getScreenshotAsURI:(CDVInvokedUrlCommand*)command {

    CGRect imageRect; CGRect screenRect = [[UIScreen mainScreen] bounds];

    // statusBarOrientation is more reliable than UIDevice.orientation UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

    if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)) { if (orientation != UIInterfaceOrientationLandscapeLeft && orientation != UIInterfaceOrientationLandscapeRight) { // landscape check imageRect = CGRectMake(0, 0, CGRectGetWidth(screenRect), CGRectGetHeight(screenRect)); } else { // portrait check imageRect = CGRectMake(0, 0, CGRectGetWidth(screenRect), CGRectGetHeight(screenRect)); }

    } else { if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { // landscape check imageRect = CGRectMake(0, 0, CGRectGetHeight(screenRect), CGRectGetWidth(screenRect)); } else { // portrait check imageRect = CGRectMake(0, 0, CGRectGetWidth(screenRect), CGRectGetHeight(screenRect)); } }

    // Adds support for Retina Display. Code reverts back to original if iOs 4 not detected. if (NULL != UIGraphicsBeginImageContextWithOptions) UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, 0); else UIGraphicsBeginImageContext(imageRect.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext(); [[UIColor blackColor] set]; CGContextTranslateCTM(ctx, 0, 0); CGContextFillRect(ctx, imageRect);

    if ([webView respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) { [webView drawViewHierarchyInRect:webView.bounds afterScreenUpdates:YES]; } else { [webView.layer renderInContext:ctx]; }

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    NSString* imageData = [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

    UIGraphicsEndImageContext();

    CDVPluginResult* pluginResult = nil;

    NSDictionary *jsonObj = [ [NSDictionary alloc] initWithObjectsAndKeys : imageData, @"URI", @"true", @"success", nil ];

    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:jsonObj];

    [self writeJavascript:[pluginResult toSuccessCallbackString:command.callbackId]];

}

tbh726 avatar May 15 '15 20:05 tbh726

sooo can someone please implement this!?!?!?!

roblav96 avatar Oct 13 '15 22:10 roblav96

it exists already if you check the ios code.

navigator.screenshot.URI(function(error,res){
  if(error){
    console.error(error);
  }else{
    html = '<img style="width:50%;" src="'+res.URI+'">';
    document.body.innerHTML = html;
  }
},50);

gitawego avatar Oct 14 '15 07:10 gitawego

omg you just saved my project!

I'm using this plugin to do native like transitions using Famous Engine to achieve 60fps transitions. I'll post the proof of concept when I'm done.

Thank you!!!

roblav96 avatar Oct 14 '15 14:10 roblav96

Actually I can't seem to get this working now.

All it does is return the same image no matter what view I change on the page :/

android 5.1

roblav96 avatar Oct 14 '15 16:10 roblav96

I'm trying to edit your code here, not too much Java experience. I'm not using crosswalk btw.

I see we are cacheing the bitmap:

            View view = webView.getView().getRootView();
        view.setDrawingCacheEnabled(true);
        bitmap = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);

I'm pretty sure this is where my problem of the same image returning every time.

roblav96 avatar Oct 14 '15 16:10 roblav96

OK so I fixed it by changing

View view = webView.getView().getRootView();

to

View view = webView.getView()

:D

roblav96 avatar Oct 14 '15 17:10 roblav96