Compare commits
36 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 096fada588 | |||
| f2d7364e2f | |||
| a4b9af06be | |||
| a7167e2271 | |||
| 2f531aaa0b | |||
| f939db234c | |||
| d48ac59654 | |||
| 73661bfde0 | |||
| 31676e30c9 | |||
| 691bd3fa40 | |||
| 2cd9da795d | |||
| 6c09ce9084 | |||
| 35bb949c91 | |||
| 9f9f4d0e1b | |||
| e53cf1eca4 | |||
| 225bd5bfc0 | |||
| faf76f2315 | |||
| 01760d5621 | |||
| d064aea90a | |||
| ff9f8edbea | |||
| b4206a4045 | |||
| c560d6ef1a | |||
| 8447803226 | |||
| 2fec5c863b | |||
| 569fbb77c4 | |||
| ae56b05219 | |||
| 80edab88e9 | |||
| 39b835717c | |||
| 4b40b56677 | |||
| e420a09a81 | |||
| e2695a6af6 | |||
| 720458fbbc | |||
| 123f36b94a | |||
| e687171a32 | |||
| 6b39e70581 | |||
| 60aee7b9fd |
@@ -18,27 +18,29 @@ So, cordova-base64-to-gallery plugin **from version 3.0.0** has changed the iOS
|
|||||||
If you need to support cordova-ios < 3.8.0 please refer to [cordova-base64-to-gallery@2.0.2](https://github.com/Nexxa/cordova-base64-to-gallery/tree/2.0.2). There is also an "**old**" branch that might have some updates in the future (Android/WP8 fixes or something like that).
|
If you need to support cordova-ios < 3.8.0 please refer to [cordova-base64-to-gallery@2.0.2](https://github.com/Nexxa/cordova-base64-to-gallery/tree/2.0.2). There is also an "**old**" branch that might have some updates in the future (Android/WP8 fixes or something like that).
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
Call the `cordova.base64ToGallery()` method using success and error callbacks and the passing the image's base64 string (`options` is optional):
|
Call the `cordova.base64ToGallery()` method with image's base64 string, success and error callbacks (`options` is optional):
|
||||||
|
|
||||||
### Methods
|
### Methods
|
||||||
#### `cordova.base64ToGallery(data, [options, success, fail])`
|
#### `cordova.base64ToGallery(data, [options, success, fail])`
|
||||||
|
|
||||||
Param | Type | Default | Description
|
Param | Type | Default | Description
|
||||||
----------- | ---------- | ----------------- | ------------------
|
----------- | ---------- | ----------------- | -----------------------------------------
|
||||||
**data** | *string* | | base64 string
|
**data** | *string* | | base64 string
|
||||||
**options** | *object* | \*see below | options
|
**options** | *object* | \*see below | options
|
||||||
**success** | *function* | **console.log** | success callback
|
**success** | *function* | **console.log** | success callback (file path as parameter)
|
||||||
**fail** | *function* | **console.error** | fail callback
|
**fail** | *function* | **console.error** | fail callback (error as parameter)
|
||||||
|
|
||||||
#### Available options *
|
#### Available options *
|
||||||
|
|
||||||
##### `prefix`
|
##### `prefix`
|
||||||
Saved file name prefix. Only works in Android and WP8.
|
Saved file name prefix.
|
||||||
|
|
||||||
**Default**: "img_"
|
**Default**: "img_"
|
||||||
|
|
||||||
##### `mediaScanner`
|
##### `mediaScanner`
|
||||||
Android Media Scanner after file creation enabled or not. Only works in Android.
|
On Android runs Media Scanner after file creation.
|
||||||
|
|
||||||
|
On iOS if true the file will be added to camera roll, otherwise will be saved to a library folder.
|
||||||
|
|
||||||
**Default**: true
|
**Default**: true
|
||||||
|
|
||||||
@@ -54,8 +56,8 @@ function onDeviceReady() {
|
|||||||
mediaScanner: true
|
mediaScanner: true
|
||||||
},
|
},
|
||||||
|
|
||||||
function(msg) {
|
function(path) {
|
||||||
console.log(msg);
|
console.log(path);
|
||||||
},
|
},
|
||||||
|
|
||||||
function(err) {
|
function(err) {
|
||||||
@@ -69,3 +71,4 @@ function onDeviceReady() {
|
|||||||
- [Tommy-Carlos Williams](http://github.com/devgeeks)
|
- [Tommy-Carlos Williams](http://github.com/devgeeks)
|
||||||
- [Simba Zhang](http://github.com/solderzzc)
|
- [Simba Zhang](http://github.com/solderzzc)
|
||||||
- [StefanoMagrassi](http://github.com/StefanoMagrassi)
|
- [StefanoMagrassi](http://github.com/StefanoMagrassi)
|
||||||
|
- [Bastian Meier](https://github.com/bastian-meier)
|
||||||
|
|||||||
+4
-3
@@ -1,12 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "cordova-base64-to-gallery",
|
"name": "cordova-base64-to-gallery",
|
||||||
"version": "4.0.0",
|
"version": "4.1.2",
|
||||||
"description": "Cordova plugin to save base64 data as a png image into the device",
|
"description": "Cordova plugin to save base64 data as a png image into the device",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"bump": "node ./scripts/bump_version",
|
"bump": "node ./scripts/bump_version",
|
||||||
"lint": "eslint www/base64ToGallery.js",
|
"lint": "eslint www/base64ToGallery.js",
|
||||||
"test": "npm run lint",
|
"test": "npm run lint",
|
||||||
|
"postversion": "npm run bump",
|
||||||
"prepublish": "npm test"
|
"prepublish": "npm test"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
@@ -56,7 +57,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "1.10.3",
|
"eslint": "3.12.2",
|
||||||
"nodemsg": "1.0.0"
|
"nodemsg": "1.1.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<plugin xmlns:android="http://schemas.android.com/apk/res/android" xmlns="http://www.phonegap.com/ns/plugins/1.0" id="cordova-base64-to-gallery" version="4.0.0">
|
<plugin xmlns:android="http://schemas.android.com/apk/res/android" xmlns="http://www.phonegap.com/ns/plugins/1.0" id="cordova-base64-to-gallery" version="4.1.2">
|
||||||
|
|
||||||
<engines>
|
<engines>
|
||||||
<engine name="cordova-ios" version=">=3.8.0" />
|
<engine name="cordova-ios" version=">=3.8.0" />
|
||||||
|
|||||||
+57
-23
@@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
//
|
//
|
||||||
// Base64ToGallery.m
|
// Base64ToGallery.m
|
||||||
// Base64ToGallery PhoneGap/Cordova plugin
|
// Base64ToGallery PhoneGap/Cordova plugin
|
||||||
@@ -20,39 +21,72 @@
|
|||||||
self.callbackId = command.callbackId;
|
self.callbackId = command.callbackId;
|
||||||
self.result = nil;
|
self.result = nil;
|
||||||
|
|
||||||
NSString* base64String = [command.arguments objectAtIndex:0];
|
NSString *base64String = [command.arguments objectAtIndex:0];
|
||||||
|
NSString *prefix = [command.arguments objectAtIndex:1];
|
||||||
|
bool cameraRoll = [[command.arguments objectAtIndex:2] boolValue];
|
||||||
|
|
||||||
if (base64String != nil && [base64String length] > 0) {
|
if (base64String != nil && [base64String length] > 0) {
|
||||||
|
|
||||||
|
NSData *imageData = [[[NSData alloc] initWithBase64EncodedString:base64String options:0] autorelease];
|
||||||
|
UIImage *image = [[[UIImage alloc] initWithData:imageData] autorelease];
|
||||||
|
|
||||||
NSData* imageData = [[[NSData alloc] initWithBase64EncodedString:base64String options:0] autorelease];
|
// converts the UIImage to NSData
|
||||||
UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease];
|
NSData *pngImageData = UIImagePNGRepresentation(image);
|
||||||
|
|
||||||
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
|
// image extension
|
||||||
|
NSString *imageExtension = @".png";
|
||||||
|
|
||||||
|
// get Timestamp
|
||||||
|
double currentTime = CACurrentMediaTime();
|
||||||
|
|
||||||
|
// set fileName
|
||||||
|
NSString *timeString = [NSString stringWithFormat:@"%f", currentTime];
|
||||||
|
timeString = [timeString stringByReplacingOccurrencesOfString:@"." withString:@""];
|
||||||
|
NSString *fileName = [prefix stringByAppendingString: timeString];
|
||||||
|
fileName = [fileName stringByAppendingString: imageExtension];
|
||||||
|
|
||||||
|
NSString *libPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
|
||||||
|
NSString *libPathNoSync = [libPath stringByAppendingPathComponent:@"NoCloud"];
|
||||||
|
NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
|
||||||
|
|
||||||
|
// Create the directory if necessary.
|
||||||
|
[fileManager createDirectoryAtPath:libPathNoSync withIntermediateDirectories:YES attributes:nil error:nil];
|
||||||
|
|
||||||
|
NSString *imagePath = [libPathNoSync stringByAppendingPathComponent:fileName];
|
||||||
|
|
||||||
|
// writeToFile
|
||||||
|
bool success = [fileManager createFileAtPath:imagePath contents:pngImageData attributes:nil];
|
||||||
|
|
||||||
|
if(success){
|
||||||
|
// write to documents folder was successfull
|
||||||
|
if(cameraRoll){
|
||||||
|
// add the image to camera roll
|
||||||
|
UIImage * savedImage = [UIImage imageWithContentsOfFile:imagePath];
|
||||||
|
UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:), nil);
|
||||||
|
}
|
||||||
|
|
||||||
|
}else{
|
||||||
|
imagePath = [imagePath stringByAppendingString: @" - error writing image to documents folder"];
|
||||||
|
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:imagePath];
|
||||||
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
self.result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
|
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"no valid base64 image data was passed"];
|
||||||
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
|
||||||
[self.commandDelegate sendPluginResult:self.result callbackId:self.callbackId];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
|
-(void)thisImage:(UIImage *)image hasBeenSavedInPhotoAlbumWithError:(NSError *)error usingContextInfo:(void*)ctxInfo{
|
||||||
{
|
if (error) {
|
||||||
// With errors
|
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Error saving Image to Gallery, check Permissions"];
|
||||||
if (error != NULL) {
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
|
||||||
NSLog(@"ERROR: %@", error);
|
} else {
|
||||||
|
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @"Image Saved to Gallery"];
|
||||||
self.result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString:error.description];
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
|
||||||
|
|
||||||
// No errors
|
|
||||||
} else {
|
|
||||||
|
|
||||||
self.result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK];
|
|
||||||
}
|
|
||||||
|
|
||||||
[self.commandDelegate sendPluginResult:self.result callbackId:self.callbackId];
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
Reference in New Issue
Block a user