Skip to content

Commit

Permalink
Allow screenshot plugin to optionally return a base64 encoded string …
Browse files Browse the repository at this point in the history
…to a given function name. Added a README for the screenshot plugin.
  • Loading branch information
l0c0luke committed Apr 29, 2011
1 parent 682744e commit cb4c09a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
31 changes: 31 additions & 0 deletions iPhone/ScreenShot/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
This plugin lets you take a screenshot of the viewable screen in iOS without the top status bar.

You can call this function with

window.navigator.saveScreenshot( bool returnBase64, [string successCallbackString] )

or

PhoneGap.exec("Screenshot.saveScreenshot", returnBase64, [ successCallbackString ] );

Where

returnBase64 (PNG format for now) is a BOOL to say whether or not we return a base64 encoded string back to Javascript or just let the screenshot get saved to the devices Saved Photos.

and

the optional successCallbackString which is the function that will be called with the returned base64 encoded string given as the only parameter.

So I used this with something like �

�.
<button onclick="takeScreenshot();">Take Screenshot</button>

function takeScreenShot() {
PhoneGap.exec("Screenshot.saveScreenshot", true, "returnScreenshotImage");
}

function returnScreenshotImage(image) {
base64string = "data:image/png;base64,"+image;
$("#image_container").css("background-image", "url("+base64string+")");
}
4 changes: 2 additions & 2 deletions iPhone/ScreenShot/ScreenShot.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ function Screenshot() {
/**
* Save the screenshot to the user's Photo Library
*/
Screenshot.prototype.saveScreenshot = function() {
PhoneGap.exec("Screenshot.saveScreenshot");
Screenshot.prototype.saveScreenshot = function( returnBase64, successCallbackString) {
PhoneGap.exec("Screenshot.saveScreenshot", returnBase64, successCallbackString );
};

PhoneGap.addConstructor(function()
Expand Down
36 changes: 32 additions & 4 deletions iPhone/ScreenShot/ScreenShot.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ @implementation Screenshot

- (void)saveScreenshot:(NSArray*)arguments withDict:(NSDictionary*)options
{
NSUInteger argc = [arguments count];
NSString* successCallback = nil, *returnBase64 = false;

if (argc > 0) returnBase64 = [arguments objectAtIndex:0];
if (argc > 1) successCallback = [arguments objectAtIndex:1];

if (argc < 1) {
NSLog(@"Screenshot.saveScreenshot: Missing 1st parameter.");
return;
}

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGRect imageRect = CGRectMake(0, 0, CGRectGetWidth(screenRect), CGRectGetHeight(screenRect));
UIGraphicsBeginImageContext(imageRect.size);
Expand All @@ -23,10 +34,27 @@ - (void)saveScreenshot:(NSArray*)arguments withDict:(NSDictionary*)options
[webView.layer renderInContext:ctx];

UIImage *image1 = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(image1, nil, nil, nil);

if (returnBase64) {
if (argc < 2) {
NSLog(@"Screenshot.saveScreenshot: Missing 2nd parameter.");
UIGraphicsEndImageContext();
return;
} else {
NSData *imageData = UIImagePNGRepresentation(image1);
NSString *encodedString = [imageData base64Encoding];
NSString *jsCallBack;
jsCallBack = [ NSString stringWithFormat:@"%@(\"%@\");", successCallback, encodedString ];
[webView stringByEvaluatingJavaScriptFromString:jsCallBack];
}
} else {
UIImageWriteToSavedPhotosAlbum(image1, nil, nil, nil);

UIAlertView *alert= [[UIAlertView alloc] initWithTitle:nil message:@"Image Saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}

UIGraphicsEndImageContext();
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:nil message:@"Image Saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
@end

1 comment on commit cb4c09a

@erikadanis81
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when i paste the .m code into XCode it errors??? HOw can i get this to work?

Please sign in to comment.