Skip to content

Instantly share code, notes, and snippets.

@Arime9
Last active April 9, 2024 07:08
Show Gist options
  • Save Arime9/b94e89adfccae06e5632b2d2395e9704 to your computer and use it in GitHub Desktop.
Save Arime9/b94e89adfccae06e5632b2d2395e9704 to your computer and use it in GitHub Desktop.
Implementation of UIImageWriteToSavedPhotosAlbum in the Cordova plugin.
#import "PhotosAlbum.h"
#import <Cordova/CDV.h>
@implementation PhotosAlbum
- (void)saveImageToPhotosAlbum:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground : ^{
NSString *base64String = [command.arguments objectAtIndex:0];
NSData *imageData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
UIImage *image = [UIImage imageWithData:imageData];
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
// selector内で成功/失敗の結果を返却するので、このスレッドは NO_RESULT, setKeepCallbackAsBool:YES で終える
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
[pluginResult setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
CDVPluginResult* pluginResult = nil;
if (error != nil) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"The image was successfully saved to your Photo Library."];
}
[pluginResult setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment