forked from Busivid/cordova-plugin-library-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibraryHelper.m
104 lines (75 loc) · 3.34 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// LibraryHelper-phonegap
// http://github.com/coryjthompson/LibraryHelper-phonegap
//
#import "LibraryHelper.h"
#import "ALAssetsLibrary+CustomPhotoAlbum.h"
@implementation LibraryHelper
@synthesize callbackId, albumName;
- (void)saveImagetoLibrary:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
self.callbackId = [arguments objectAtIndex: 0];
NSString* path = [arguments objectAtIndex: 1];
self.albumName = [arguments objectAtIndex: 2];
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);
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
if([self.albumName isEqualToString:@"null"]){
self.albumName = NULL;
}
[library saveImage:image toAlbum: self.albumName completionBlock:^(NSURL *assetURL, NSError *error) {
[self assetSavedToLibrary:error];
} failureBlock:^(NSError *error) {
[self assetSavedToLibrary:error];
}];
}
- (void)saveVideoToLibrary:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
self.callbackId = [arguments objectAtIndex: 0];
NSString* path = [arguments objectAtIndex:1];
self.albumName = [arguments objectAtIndex: 2];
if (!path || !UIVideoAtPathIsCompatibleWithSavedPhotosAlbum( path ) )
{
CDVPluginResult* pluginResult;
//Video path not valid
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: @"Path not a valid video file"];
[self writeJavascript: [pluginResult toErrorCallbackString:self.callbackId]];
return;
}
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
if([self.albumName isEqualToString:@"null"]){
self.albumName = NULL;
}
NSURL *url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[library saveVideo:url toAlbum:self.albumName completionBlock:^(NSURL *assetURL, NSError *error) {
[self assetSavedToLibrary:error];
} failureBlock:^(NSError *error) {
[self assetSavedToLibrary:error];
}];
}
- (void)assetSavedToLibrary:(NSError *)error {
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]];
}
- (void)dealloc {
[self.callbackId release];
[super dealloc];
}
@end