科尔多瓦截图插件无法返回图像

Posted

技术标签:

【中文标题】科尔多瓦截图插件无法返回图像【英文标题】:cordova screenshot plugin cant return image 【发布时间】:2012-07-12 19:21:40 【问题描述】:

我正在使用 Cordova/jQuery Mobile 构建一个 iPhone/iPad 应用程序。我使用了以下截图插件https://github.com/josemando/phonegap-plugins/commit/4c2ec54ae001ad409e4d4fe6f5ec6b9ef8b7dc3f,它很有效。它捕获屏幕截图并将其保存到我的相机胶卷相册中。但是,我怎样才能在我的 javascript 中获取此图像以最终将其上传到我的网络服务器上。

我使用下面的js脚本来执行截图功能,但是没有返回图片。

Screenshot.prototype.saveScreenshot = function() 
    cordovaRef.exec("Screenshot.saveScreenshot");
;

这是 Screenshot.m 文件:

#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

【问题讨论】:

【参考方案1】:

我认为这行不通!

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

例如:

UIImageWriteToSavedPhotosAlbum(theImage,
   self, // send the message to 'self' when calling the callback
   @selector(thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:), // the selector to tell the method to call on completion
   NULL); // you generally won't need a contextInfo here

look here

获取图片

- (UIImage*)saveScreenshot:(NSArray*)arguments withDict:(NSDictionary*)options
....
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

....
 UIGraphicsEndImageContext();
....

return image;

【讨论】:

以上是关于科尔多瓦截图插件无法返回图像的主要内容,如果未能解决你的问题,请参考以下文章

电话间隙闪屏插件不显示图像

如何使用科尔多瓦相机插件重播录制的视频?

插件返回后 Cordova 视图无响应

openSettings 插件科尔多瓦

如何检测屏幕截图事件并在科尔多瓦中停止它

科尔多瓦相机插件在 ios 8.1 中不起作用