Reputation: 119
I'm trying to use iOS PhoneGap Screenshot plugin on PhoneGap 2.1.
This is my Screenshot.m file (put in Plugind folder).
//
// 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
This is my Screenshot.h file, put in Plugins folder :
//
// 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>
#import <Cordova/CDVPlugin.h>
@interface Screenshot : CDVPlugin {
}
- (void)saveScreenshot:(NSArray*)arguments withDict:(NSDictionary*)options;
@end
This is my Screenshot.js file put in www folder
/*
* 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-07-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(null, null, "Screenshot", "saveScreenshot", []);
};
cordovaRef.addConstructor(function() {
if (!window.plugins) {
window.plugins = {};
}
if (!window.plugins.Screenshot) {
window.plugins.Screenshot = new Screenshot();
}
});
})(); /* End of Temporary Scope. */
I call my function is script.js with this line : window.plugins.Screenshot.saveScreenshot() ;
I've already added Screenshot plugin in Cordova.plist
Now I'm running application, no compile error, but JS doesn't works and I've no screenshot in image app.
Thanks for your help
Upvotes: 1
Views: 2130
Reputation: 53301
I think you didn't paste the Script.js code, but I think I know what your problem is. You are calling the plugin on document ready, but to call a plugin you have to wait for device ready
document.addEventListener("deviceready",onDeviceReady,false);
// Cordova is ready to be used!
//
function onDeviceReady() {
window.plugins.Screenshot.saveScreenshot() ;
}
Upvotes: 1