forked from Busivid/cordova-plugin-library-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibraryHelper.m
72 lines (52 loc) · 2.54 KB
/
LibraryHelper.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// LibraryHelper-phonegap
// https://github.com/coryjthompson/LibraryHelper-phonegap
//
// Author: Cory Thompson (http://coryjthompson.com)
// License: http://www.opensource.org/licenses/mit-license.php The MIT License
#import "LibraryHelper.h"
@implementation LibraryHelper
@synthesize callbackId;
- (void)saveImagetoLibrary:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
self.callbackId = [arguments objectAtIndex: 0];
NSString* path = [arguments objectAtIndex: 1];
CDVPluginResult* pluginResult;
if(!path){
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: @"Path cannot be null"];
[self writeJavascript: [pluginResult toErrorCallbackString:self.callbackId]];
return;
}
UIImage *image = [UIImage imageWithContentsOfFile:path];
if(!image){
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: @"Path not a valid image file"];
[self writeJavascript: [pluginResult toErrorCallbackString:self.callbackId]];
return;
}
UIImageWriteToSavedPhotosAlbum(image, self, @selector(assetSavedToLibrary:didFinishSavingWithError:contextInfo:), nil);
}
- (void)saveVideoToLibrary:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
self.callbackId = [arguments objectAtIndex: 0];
NSString* path = [arguments objectAtIndex:1];
CDVPluginResult* pluginResult;
if (!path || !UIVideoAtPathIsCompatibleWithSavedPhotosAlbum( path ) )
{
//Video path not valid
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: @"Path not a valid video file"];
[self writeJavascript: [pluginResult toErrorCallbackString:self.callbackId]];
return;
}
UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(assetSavedToLibrary:didFinishSavingWithError:contextInfo:), nil);
}
- (void)assetSavedToLibrary:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
CDVPluginResult* pluginResult;
if (error) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: [error description]];
[self writeJavascript: [pluginResult toErrorCallbackString:self.callbackId]];
return;
}
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"Successfully Added to Library"];
[self writeJavascript: [pluginResult toSuccessCallbackString:self.callbackId]];
}
@end