Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Updated Screenshot plugin to Cordova 1.6.1 #600

Merged
merged 2 commits into from

3 participants

@josemando

Hello,
I've updated the iOS Screenshot plugin to work with Cordova 1.6.1
Do I need to open a issue for that ?

@josemando

anyone?

@RandyMcMillan RandyMcMillan merged commit b0981f8 into phonegap:master
@erikadanis81

hello,

How can i get the Screenshot that's just been saved to my albums to be able to upload it? Can i return the image in the javascript?

Many Thanks

if you don't need to have the screenshot saved in the photo album, you can edit the Screenshot.m and upload the image to wherever you want.
In order to return it to the javascript, I believe you would just have to encode it to base64

Thanks alot for your response :) Im not really familiar with objective C, hence why im using Cordova/jquery...how would i amend Screenshot.m to return it to javascript? Also is it possible to save it both to the photo Album and return it to the javascript?

Many Thanks

in order to have Screenshot.m both saving the screenshot to the album and returning it to the javascript, you just have to add the code to encode the image into base64. i did it once, but i end up giving it up and upload the picture straight using object-c.
if i recall correctly there some fork around of a guy who did this...

My primary focus is to return the image back to the JavaScript....saving it to the photo album is a bonus! Have u got any example scripts I could add to the screenshot.m as I wouldn't know where to start. I've been researching online 2 days now and haven't got any further.
What did u mean about a guy who's done a fork? Any idea where I could find it?

Take a look at github.com/l0c0luke/phonegap-plugins
There's code that to do exactly what you need :)

Thanks josemando!! I managed to find his code this morning and implemented :) It worked a treat apart from a warning i get in Xcode that i cant get rid off...even tho its working when i run it on my phone!

Warning : " Instance method '-base64Encoding' not found (return type defaults to 'id') "

you can get rid of it if you add this import to Screenshot.m:

#import "NSData+Base64.h"

Just added that and now its complaining about the following:

Error: " NSData+Base64.h' file not found "

Im building my app using Cordova/PhoneGap

That's odd. NSData+Base64.h is included in Cordova framework.
You're placing the import at the beginning of the class? Right after "Screenshot.h" import?

Yes I put it under screenshot.h I've found an NSData+Base64.h file and added it to my plugins bits that's caused other warnings and errors. I'll have a look at it over the weekend. Really appreciate ur help so far :).

you don't need to place the file at the plugins folder. since it's inside Cordova.framework your application should already have access to it.
well, if you are only getting a warning, you could just leave it as is :) you're welcome and good luck with the project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
3  iOS/README.md
@@ -16,6 +16,7 @@ The present https://github.com/phonegap/phonegap-plugins/tree/master/iPhone shou
* Added a NativeControls (iOS) plugin with Cordova support.
* Added a PayPalPlugin (iOS) plugin with Cordova support.
* Added a PrintPlugin (iOS) plugin with Cordova support.
+* Added a Screenshot (iOS) plugin with Cordova support.
* Added a SMSComposer (iOS) plugin with Cordova support.
* Added a Twitter (iOS) plugin with Cordova support.
* Added a VolumeSlider (iOS) plugin with Cordova support.
@@ -90,4 +91,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
24 iOS/Screenshot/Screenshot.h
@@ -0,0 +1,24 @@
+//
+// Screenshot.h
+//
+// Created by Simon Madine on 29/04/2010.
+// Copyright 2010 The Angry Robot Zombie Factory.
+// - Converted to Cordova 1.6.1 by Josemando Sobral.
+// MIT licensed
+//
+
+#import <Foundation/Foundation.h>
+#import <QuartzCore/QuartzCore.h>
+
+#ifdef CORDOVA_FRAMEWORK
+ #import <Cordova/CDVPlugin.h>
+#else
+ #import "CDVPlugin.h"
+#endif
+
+@interface Screenshot : CDVPlugin {
+}
+
+- (void)saveScreenshot:(NSArray*)arguments withDict:(NSDictionary*)options;
+
+@end
View
40 iOS/Screenshot/Screenshot.js
@@ -0,0 +1,40 @@
+/*
+ * This code is adapted from the work of Michael Nachbaur
+ * by Simon Madine of The Angry Robot Zombie Factory
+ * - Converted to Cordova 1.6.1 by Josemando Sobral.
+ * 2012-06-03
+ * MIT licensed
+ */
+
+/*
+ * Temporary Scope to contain the plugin.
+ * - More information here:
+ * https://github.com/apache/incubator-cordova-ios/blob/master/guides/Cordova%20Plugin%20Upgrade%20Guide.md
+ */
+(function() {
+ /* Get local ref to global PhoneGap/Cordova/cordova object for exec function.
+ - This increases the compatibility of the plugin. */
+ var cordovaRef = window.PhoneGap || window.Cordova || window.cordova; // old to new fallbacks
+
+ /**
+ * This class exposes the ability to take a Screenshot to JavaScript
+ */
+ function Screenshot() { }
+
+ /**
+ * Save the screenshot to the user's Photo Library
+ */
+ Screenshot.prototype.saveScreenshot = function() {
+ cordovaRef.exec("Screenshot.saveScreenshot");
+ };
+
+ cordovaRef.addConstructor(function() {
+ if (!window.plugins) {
+ window.plugins = {};
+ }
+ if (!window.plugins.screenshot) {
+ window.plugins.screenshot = new Screenshot();
+ }
+ });
+
+ })(); /* End of Temporary Scope. */
View
52 iOS/Screenshot/Screenshot.m
@@ -0,0 +1,52 @@
+//
+// Screenshot.h
+//
+// Created by Simon Madine on 29/04/2010.
+// Copyright 2010 The Angry Robot Zombie Factory.
+// - Converted to Cordova 1.6.1 by Josemando Sobral.
+// MIT licensed
+//
+// Modifications to support orientation change by @ffd8
+//
+
+#import "Screenshot.h"
+
+@implementation Screenshot
+
+@synthesize webView;
+
+- (void)saveScreenshot:(NSArray*)arguments withDict:(NSDictionary*)options
+{
+ CGRect imageRect;
+ CGRect screenRect = [[UIScreen mainScreen] bounds];
+
+ // statusBarOrientation is more reliable than UIDevice.orientation
+ UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
+
+ 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));
+ }
+
+ UIGraphicsBeginImageContext(imageRect.size);
+
+ CGContextRef ctx = UIGraphicsGetCurrentContext();
+ [[UIColor blackColor] set];
+ CGContextTranslateCTM(ctx, 0, 0);
+ CGContextFillRect(ctx, imageRect);
+
+ [webView.layer renderInContext:ctx];
+
+ UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
+ UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
+ UIGraphicsEndImageContext();
+
+ UIAlertView *alert= [[UIAlertView alloc] initWithTitle:nil message:@"Image Saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
+ [alert show];
+ [alert release];
+}
+
+@end
Something went wrong with that request. Please try again.