|
| 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 |
0 commit comments