Skip to content

Commit 2fdc366

Browse files
Supports AssetsLibrary.framework
1 parent 16085a9 commit 2fdc366

21 files changed

+1150
-343
lines changed

Classes/JSBScriptingSupport.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,21 @@
1717
+ (JSContext *)globalContext;
1818

1919
JSExportAs(defineClass,
20-
+ (id)defineClass:(NSString *)declaration
21-
instanceMembers:(JSValue *)instanceMembers
22-
staticMembers:(JSValue *)staticMembers
23-
);
20+
+ (id)defineClass:(NSString *)declaration instanceMembers:(JSValue *)instanceMembers staticMembers:(JSValue *)staticMembers);
2421

2522
JSExportAs(require,
26-
+ (id)require:(NSString *)name
27-
);
23+
+ (id)require:(NSString *)name);
24+
25+
JSExportAs(dispatch_async,
26+
+ (void)dispatch_async:(id)queue block:(JSValue *)block);
27+
28+
JSExportAs(dispatch_get_global_queue,
29+
+ (id)dispatch_get_global_queue:(dispatch_queue_priority_t)priority flags:(unsigned long)flags);
30+
+ (id)dispatch_get_main_queue;
2831

2932
@optional
3033
JSExportAs(dump,
31-
+ (void)dump:(id)object
32-
);
34+
+ (void)dump:(id)object);
3335

3436
@end
3537

Classes/JSBScriptingSupport.m

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ + (void)initialize
4545
@" selector: function(str) {\n"
4646
@" return __JSB_JSBScriptingSupport.selectorFromString(str);\n"
4747
@" },\n"
48+
@" dispatch_async: function(queue, block) {\n"
49+
@" return __JSB_JSBScriptingSupport.dispatch_async(queue, block);\n"
50+
@" },\n"
51+
@" dispatch_get_global_queue: function(priority, flags) {\n"
52+
@" return __JSB_JSBScriptingSupport.dispatch_get_global_queue(priority, flags);\n"
53+
@" },\n"
54+
@" dispatch_get_main_queue: function() {\n"
55+
@" return __JSB_JSBScriptingSupport.dispatch_get_main_queue();\n"
56+
@" },\n"
4857
@" dump: function(obj) {\n"
4958
@" return __JSB_JSBScriptingSupport.dump(obj);\n"
5059
@" }\n"
@@ -63,9 +72,9 @@ + (JSContext *)globalContext
6372
return globalContext;
6473
}
6574

66-
+ (id)defineClass:(NSString *)declaration
67-
instanceMembers:(JSValue *)instanceMembers
68-
staticMembers:(JSValue *)staticMembers
75+
#pragma mark -
76+
77+
+ (id)defineClass:(NSString *)declaration instanceMembers:(JSValue *)instanceMembers staticMembers:(JSValue *)staticMembers
6978
{
7079
NSScanner *scanner = [NSScanner scannerWithString:declaration];
7180

@@ -143,6 +152,31 @@ + (id)require:(NSString *)name
143152
return module;
144153
}
145154

155+
#pragma mark -
156+
157+
+ (void)dispatch_async:(id)queue block:(JSValue *)function
158+
{
159+
dispatch_block_t block = NULL;
160+
if (!function.isUndefined) {
161+
block = ^() {
162+
[function callWithArguments:nil];
163+
};
164+
}
165+
166+
dispatch_async(queue, block);
167+
}
168+
169+
+ (id)dispatch_get_global_queue:(dispatch_queue_priority_t)priority
170+
flags:(unsigned long)flags
171+
{
172+
return dispatch_get_global_queue(priority, flags);
173+
}
174+
175+
+ (id)dispatch_get_main_queue
176+
{
177+
return dispatch_get_main_queue();
178+
}
179+
146180
#pragma mark - for debug
147181

148182
+ (void)log:(NSString *)format, ...
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// ALAsset+JavaScriptBridge.h
3+
// JavaScriptBridge
4+
//
5+
// Created by kishikawa katsumi on 2014/01/05.
6+
// Copyright (c) 2014 kishikawa katsumi. All rights reserved.
7+
//
8+
9+
#import <AssetsLibrary/AssetsLibrary.h>
10+
11+
@interface ALAsset (JavaScriptBridge)
12+
13+
@end
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
//
2+
// ALAsset+JavaScriptBridge.m
3+
// JavaScriptBridge
4+
//
5+
// Created by kishikawa katsumi on 2014/01/05.
6+
// Copyright (c) 2014 kishikawa katsumi. All rights reserved.
7+
//
8+
9+
#import "ALAsset+JavaScriptBridge.h"
10+
#import "JSBMessageForwarding.h"
11+
12+
@implementation ALAsset (JavaScriptBridge)
13+
14+
- (void)__writeModifiedImageDataToSavedPhotosAlbum:(NSData *)imageData
15+
metadata:(NSDictionary *)metadata
16+
completionBlock:(JSValue *)completionFunction
17+
{
18+
JSContext *context = [JSContext currentContext];
19+
id currentSelf = context[@"self"];
20+
21+
ALAssetsLibraryWriteImageCompletionBlock completionBlock = NULL;
22+
if (!completionFunction.isUndefined) {
23+
completionBlock = ^(NSURL *assetURL, NSError *error) {
24+
NSMutableArray *parameters = [[NSMutableArray alloc] init];
25+
if (assetURL) {
26+
[parameters addObject:assetURL];
27+
} else {
28+
[parameters addObject:[NSNull null]];
29+
}
30+
if (error) {
31+
[parameters addObject:error];
32+
} else {
33+
[parameters addObject:[NSNull null]];
34+
}
35+
36+
dispatchFunction(currentSelf, completionFunction, parameters);
37+
};
38+
}
39+
40+
[self writeModifiedImageDataToSavedPhotosAlbum:imageData metadata:metadata completionBlock:completionBlock];
41+
}
42+
43+
- (void)__writeModifiedVideoAtPathToSavedPhotosAlbum:(NSURL *)videoPathURL
44+
completionBlock:(JSValue *)completionFunction
45+
{
46+
JSContext *context = [JSContext currentContext];
47+
id currentSelf = context[@"self"];
48+
49+
ALAssetsLibraryWriteVideoCompletionBlock completionBlock = NULL;
50+
if (!completionFunction.isUndefined) {
51+
completionBlock = ^(NSURL *assetURL, NSError *error) {
52+
NSMutableArray *parameters = [[NSMutableArray alloc] init];
53+
if (assetURL) {
54+
[parameters addObject:assetURL];
55+
} else {
56+
[parameters addObject:[NSNull null]];
57+
}
58+
if (error) {
59+
[parameters addObject:error];
60+
} else {
61+
[parameters addObject:[NSNull null]];
62+
}
63+
64+
dispatchFunction(currentSelf, completionFunction, parameters);
65+
};
66+
}
67+
68+
[self writeModifiedVideoAtPathToSavedPhotosAlbum:videoPathURL completionBlock:completionBlock];
69+
}
70+
71+
- (void)__setImageData:(NSData *)imageData
72+
metadata:(NSDictionary *)metadata
73+
completionBlock:(JSValue *)completionFunction
74+
{
75+
JSContext *context = [JSContext currentContext];
76+
id currentSelf = context[@"self"];
77+
78+
ALAssetsLibraryWriteImageCompletionBlock completionBlock = NULL;
79+
if (!completionFunction.isUndefined) {
80+
completionBlock = ^(NSURL *assetURL, NSError *error) {
81+
NSMutableArray *parameters = [[NSMutableArray alloc] init];
82+
if (assetURL) {
83+
[parameters addObject:assetURL];
84+
} else {
85+
[parameters addObject:[NSNull null]];
86+
}
87+
if (error) {
88+
[parameters addObject:error];
89+
} else {
90+
[parameters addObject:[NSNull null]];
91+
}
92+
93+
dispatchFunction(currentSelf, completionFunction, parameters);
94+
};
95+
}
96+
97+
[self setImageData:imageData metadata:metadata completionBlock:completionBlock];
98+
}
99+
100+
- (void)__setVideoAtPath:(NSURL *)videoPathURL
101+
completionBlock:(JSValue *)completionFunction
102+
{
103+
JSContext *context = [JSContext currentContext];
104+
id currentSelf = context[@"self"];
105+
106+
ALAssetsLibraryWriteVideoCompletionBlock completionBlock = NULL;
107+
if (!completionFunction.isUndefined) {
108+
completionBlock = ^(NSURL *assetURL, NSError *error) {
109+
NSMutableArray *parameters = [[NSMutableArray alloc] init];
110+
if (assetURL) {
111+
[parameters addObject:assetURL];
112+
} else {
113+
[parameters addObject:[NSNull null]];
114+
}
115+
if (error) {
116+
[parameters addObject:error];
117+
} else {
118+
[parameters addObject:[NSNull null]];
119+
}
120+
121+
dispatchFunction(currentSelf, completionFunction, parameters);
122+
};
123+
}
124+
125+
[self setVideoAtPath:videoPathURL completionBlock:completionBlock];
126+
}
127+
128+
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// ALAssetsGroup+JavaScriptBridge.h
3+
// JavaScriptBridge
4+
//
5+
// Created by kishikawa katsumi on 2014/01/05.
6+
// Copyright (c) 2014年 kishikawa katsumi. All rights reserved.
7+
//
8+
9+
#import <AssetsLibrary/AssetsLibrary.h>
10+
11+
@interface ALAssetsGroup (JavaScriptBridge)
12+
13+
@end
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//
2+
// ALAssetsGroup+JavaScriptBridge.m
3+
// JavaScriptBridge
4+
//
5+
// Created by kishikawa katsumi on 2014/01/05.
6+
// Copyright (c) 2014年 kishikawa katsumi. All rights reserved.
7+
//
8+
9+
#import "ALAssetsGroup+JavaScriptBridge.h"
10+
#import "JSBMessageForwarding.h"
11+
12+
@implementation ALAssetsGroup (JavaScriptBridge)
13+
14+
- (void)__enumerateAssetsUsingBlock:(JSValue *)enumerationFunction
15+
{
16+
JSContext *context = [JSContext currentContext];
17+
id currentSelf = context[@"self"];
18+
19+
ALAssetsGroupEnumerationResultsBlock enumerationBlock = NULL;
20+
if (!enumerationFunction.isUndefined) {
21+
enumerationBlock = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
22+
NSMutableArray *parameters = [[NSMutableArray alloc] init];
23+
if (result) {
24+
[parameters addObject:result];
25+
} else {
26+
[parameters addObject:[NSNull null]];
27+
}
28+
if (index != NSNotFound) {
29+
[parameters addObject:@(index)];
30+
} else {
31+
[parameters addObject:[NSNull null]];
32+
}
33+
34+
dispatchFunction(currentSelf, enumerationFunction, parameters);
35+
};
36+
}
37+
38+
[self enumerateAssetsUsingBlock:enumerationBlock];
39+
}
40+
41+
- (void)__enumerateAssetsWithOptions:(NSEnumerationOptions)options
42+
usingBlock:(JSValue *)enumerationFunction
43+
{
44+
JSContext *context = [JSContext currentContext];
45+
id currentSelf = context[@"self"];
46+
47+
ALAssetsGroupEnumerationResultsBlock enumerationBlock = NULL;
48+
if (!enumerationFunction.isUndefined) {
49+
enumerationBlock = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
50+
NSMutableArray *parameters = [[NSMutableArray alloc] init];
51+
if (result) {
52+
[parameters addObject:result];
53+
} else {
54+
[parameters addObject:[NSNull null]];
55+
}
56+
if (index != NSNotFound) {
57+
[parameters addObject:@(index)];
58+
} else {
59+
[parameters addObject:[NSNull null]];
60+
}
61+
62+
dispatchFunction(currentSelf, enumerationFunction, parameters);
63+
};
64+
}
65+
66+
[self enumerateAssetsWithOptions:options usingBlock:enumerationBlock];
67+
}
68+
69+
- (void)__enumerateAssetsAtIndexes:(NSIndexSet *)indexSet
70+
options:(NSEnumerationOptions)options
71+
usingBlock:(JSValue *)enumerationFunction
72+
{
73+
JSContext *context = [JSContext currentContext];
74+
id currentSelf = context[@"self"];
75+
76+
ALAssetsGroupEnumerationResultsBlock enumerationBlock = NULL;
77+
if (!enumerationFunction.isUndefined) {
78+
enumerationBlock = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
79+
NSMutableArray *parameters = [[NSMutableArray alloc] init];
80+
if (result) {
81+
[parameters addObject:result];
82+
} else {
83+
[parameters addObject:[NSNull null]];
84+
}
85+
if (index != NSNotFound) {
86+
[parameters addObject:@(index)];
87+
} else {
88+
[parameters addObject:[NSNull null]];
89+
}
90+
91+
dispatchFunction(currentSelf, enumerationFunction, parameters);
92+
};
93+
}
94+
95+
[self enumerateAssetsAtIndexes:indexSet options:options usingBlock:enumerationBlock];
96+
}
97+
98+
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// ALAssetsLibrary+JavaScriptBridge.h
3+
// JavaScriptBridge
4+
//
5+
// Created by kishikawa katsumi on 2014/01/05.
6+
// Copyright (c) 2014 kishikawa katsumi. All rights reserved.
7+
//
8+
9+
#import <AssetsLibrary/AssetsLibrary.h>
10+
11+
@interface ALAssetsLibrary (JavaScriptBridge)
12+
13+
@end

0 commit comments

Comments
 (0)