【问题标题】:iOS Screenshot plugin on PhoneGap 2.1PhoneGap 2.1 上的 iOS 截图插件
【发布时间】:2012-11-05 11:29:15
【问题描述】:

我正在尝试在 PhoneGap 2.1 上使用 iOS PhoneGap Screenshot 插件。

这是我的 Screenshot.m 文件(放在 Plugind 文件夹中)。

//
//  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

这是我的 Screenshot.h 文件,放在 Plugins 文件夹中:

//
//  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

这是我放在 www 文件夹中的 Screenshot.js 文件

 /*
 *  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. */

我用这一行调用我的函数是 script.js:window.plugins.Screenshot.saveScreenshot() ;

我已经在 Cordova.plist 中添加了截图插件

现在我正在运行应用程序,没有编译错误,但是 JS 不起作用,我在图像应用程序中没有截图。

感谢您的帮助

【问题讨论】:

  • 你在Cordova.plist中添加了相应的条目吗? 截图截图
  • 您也可以发布 script.js 内容吗?或者它只包含 window.plugins.Screenshot.saveScreenshot();?

标签: ios cordova screenshot


【解决方案1】:

我认为您没有粘贴 Script.js 代码,但我想我知道您的问题是什么。 您在文档就绪时调用插件,但要调用插件,您必须等待设备就绪

document.addEventListener("deviceready",onDeviceReady,false);

    // Cordova is ready to be used!
    //
    function onDeviceReady() {
        window.plugins.Screenshot.saveScreenshot() ;
    }

【讨论】:

  • 我在script.js的开头有“$(document).ready(function(){”。但我不明白为什么这个插件不起作用,因为我也使用通知插件它有效。
  • 是的,你有$(document).ready,但是你也需要deviceready,有时候文档已经准备好了,但是设备还没有,你需要等待deviceready才能使用插入。当文档准备好时,您正在调用屏幕截图插件,可能设备还没有准备好。当您稍后调用通知插件时,设备已准备就绪并且可以正常工作。无论如何,如果您尝试使用设备就绪但仍然无法正常工作,我会在今天下午检查插件代码。
  • 好的,我明白了这个问题!非常感谢 !它只是缺少设备准备好你是对的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-01
  • 1970-01-01
相关资源
最近更新 更多