Merge branch 'master' of github.com:itinance/react-native-fs
* 'master' of github.com:itinance/react-native-fs: Added copyAssetsFileIOS to support asset-library-Files from camera-roll in iOS
This commit is contained in:
commit
f98be8fd8e
|
@ -274,6 +274,15 @@ var RNFS = {
|
||||||
return RNFSManager.copyFileAssets(normalizeFilePath(filepath), normalizeFilePath(destPath)).then(() => void 0);
|
return RNFSManager.copyFileAssets(normalizeFilePath(filepath), normalizeFilePath(destPath)).then(() => void 0);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// iOS only
|
||||||
|
// Copies fotos from asset-library (camera-roll) to a specific location
|
||||||
|
// with a given width or height
|
||||||
|
// @see: https://developer.apple.com/reference/photos/phimagemanager/1616964-requestimageforasset
|
||||||
|
copyAssetsFileIOS(imageUri: string, destPath: string, width: number, height: number,
|
||||||
|
scale : number = 1.0, compression : number = 1.0, resizeMode : string = 'contain' ): Promise<string> {
|
||||||
|
return RNFSManager.copyAssetsFileIOS(imageUri, destPath, width, height, scale, compression, resizeMode );
|
||||||
|
},
|
||||||
|
|
||||||
writeFile(filepath: string, contents: string, encodingOrOptions?: any): Promise<void> {
|
writeFile(filepath: string, contents: string, encodingOrOptions?: any): Promise<void> {
|
||||||
var b64;
|
var b64;
|
||||||
|
|
||||||
|
|
12
README.md
12
README.md
|
@ -381,10 +381,20 @@ Note: On Android copyFile will overwrite `destPath` if it already exists. On iOS
|
||||||
|
|
||||||
### `copyFileAssets(filepath: string, destPath: string): Promise<void>`
|
### `copyFileAssets(filepath: string, destPath: string): Promise<void>`
|
||||||
|
|
||||||
Copies the file at `filepath ` in the Android app's assets folder and copies it to the given `destPath ` path.
|
Copies the file at `filepath` in the Android app's assets folder and copies it to the given `destPath ` path.
|
||||||
|
|
||||||
Note: Android only. Will overwrite destPath if it already exists
|
Note: Android only. Will overwrite destPath if it already exists
|
||||||
|
|
||||||
|
### `copyAssetsFileIOS(imageUri: string, destPath: string, width: number, height: number, scale : number = 1.0, compression : number = 1.0, resizeMode : string = 'contain' ): Promise<string>`
|
||||||
|
|
||||||
|
iOS-ony: copies a file from camera-roll, that is prefixed with "assets-library://asset/asset.JPG?..."
|
||||||
|
to a specific destination. It will download the original from iCloud if necessary.
|
||||||
|
If width and height is > 0, the image will be resized to a specific size and a specific compression rate.
|
||||||
|
If scale is below 1, the image will be scaled according to the scale-factor (between 0.0 and 1.0)
|
||||||
|
The resizeMode is also considered.
|
||||||
|
Further information: https://developer.apple.com/reference/photos/phimagemanager/1616964-requestimageforasset
|
||||||
|
The promise will on success return the final destination of the file, as it was defined in the destPath-parameter.
|
||||||
|
|
||||||
### `unlink(filepath: string): Promise<void>`
|
### `unlink(filepath: string): Promise<void>`
|
||||||
|
|
||||||
Unlinks the item at `filepath`. If the item does not exist, an error will be thrown.
|
Unlinks the item at `filepath`. If the item does not exist, an error will be thrown.
|
||||||
|
|
|
@ -6,17 +6,8 @@
|
||||||
// Copyright (c) 2015 Johannes Lumpe. All rights reserved.
|
// Copyright (c) 2015 Johannes Lumpe. All rights reserved.
|
||||||
//
|
//
|
||||||
|
|
||||||
#if __has_include("RCTBridgeModule.h")
|
|
||||||
#import "RCTBridgeModule.h"
|
|
||||||
#else
|
|
||||||
#import <React/RCTBridgeModule.h>
|
#import <React/RCTBridgeModule.h>
|
||||||
#endif
|
|
||||||
|
|
||||||
#if __has_include("RCTLog.h")
|
|
||||||
#import "RCTLog.h"
|
|
||||||
#else
|
|
||||||
#import <React/RCTLog.h>
|
#import <React/RCTLog.h>
|
||||||
#endif
|
|
||||||
|
|
||||||
@interface RNFSManager : NSObject <RCTBridgeModule>
|
@interface RNFSManager : NSObject <RCTBridgeModule>
|
||||||
|
|
||||||
|
|
|
@ -12,13 +12,13 @@
|
||||||
#import "Downloader.h"
|
#import "Downloader.h"
|
||||||
#import "Uploader.h"
|
#import "Uploader.h"
|
||||||
|
|
||||||
#if __has_include("RCTEventDispatcher.h")
|
|
||||||
#import "RCTEventDispatcher.h"
|
|
||||||
#else
|
|
||||||
#import <React/RCTEventDispatcher.h>
|
#import <React/RCTEventDispatcher.h>
|
||||||
#endif
|
#import <React/RCTUtils.h>
|
||||||
|
#import <React/RCTImageLoader.h>
|
||||||
|
|
||||||
#import <CommonCrypto/CommonDigest.h>
|
#import <CommonCrypto/CommonDigest.h>
|
||||||
|
#import <Photos/Photos.h>
|
||||||
|
|
||||||
|
|
||||||
@interface RNFSManager()
|
@interface RNFSManager()
|
||||||
|
|
||||||
|
@ -503,6 +503,90 @@ RCT_EXPORT_METHOD(getFSInfo:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromise
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* iOS Only: copy images from the assets-library (camera-roll) to a specific path, asuming
|
||||||
|
* JPEG-Images. Video-Support will follow up, not implemented yet.
|
||||||
|
*
|
||||||
|
* It is also supported to scale the image via scale-factor (0.0-1.0) or with a specific
|
||||||
|
* width and height. Also the resizeMode will be considered.
|
||||||
|
*/
|
||||||
|
RCT_EXPORT_METHOD(copyAssetsFileIOS: (NSString *) imageUri
|
||||||
|
toFilepath: (NSString *) destination
|
||||||
|
width: (NSInteger) width
|
||||||
|
height: (NSInteger) height
|
||||||
|
scale: (CGFloat) scale
|
||||||
|
compression: (CGFloat) compression
|
||||||
|
resizeMode: (RCTResizeMode) resizeMode
|
||||||
|
resolver: (RCTPromiseResolveBlock) resolve
|
||||||
|
rejecter: (RCTPromiseRejectBlock) reject)
|
||||||
|
|
||||||
|
{
|
||||||
|
CGSize size = CGSizeMake(width, height);
|
||||||
|
|
||||||
|
NSURL* url = [NSURL URLWithString:imageUri];
|
||||||
|
PHFetchResult *results = [PHAsset fetchAssetsWithALAssetURLs:@[url] options:nil];
|
||||||
|
|
||||||
|
if (results.count == 0) {
|
||||||
|
NSString *errorText = [NSString stringWithFormat:@"Failed to fetch PHAsset with local identifier %@ with no error message.", imageUri];
|
||||||
|
|
||||||
|
NSMutableDictionary* details = [NSMutableDictionary dictionary];
|
||||||
|
[details setValue:errorText forKey:NSLocalizedDescriptionKey];
|
||||||
|
NSError *error = [NSError errorWithDomain:@"RNFS" code:500 userInfo:details];
|
||||||
|
[self reject: reject withError:error];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
PHAsset *asset = [results firstObject];
|
||||||
|
PHImageRequestOptions *imageOptions = [PHImageRequestOptions new];
|
||||||
|
|
||||||
|
// Allow us to fetch images from iCloud
|
||||||
|
imageOptions.networkAccessAllowed = YES;
|
||||||
|
|
||||||
|
|
||||||
|
// Note: PhotoKit defaults to a deliveryMode of PHImageRequestOptionsDeliveryModeOpportunistic
|
||||||
|
// which means it may call back multiple times - we probably don't want that
|
||||||
|
imageOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
|
||||||
|
|
||||||
|
BOOL useMaximumSize = CGSizeEqualToSize(size, CGSizeZero);
|
||||||
|
CGSize targetSize;
|
||||||
|
if (useMaximumSize) {
|
||||||
|
targetSize = PHImageManagerMaximumSize;
|
||||||
|
imageOptions.resizeMode = PHImageRequestOptionsResizeModeNone;
|
||||||
|
} else {
|
||||||
|
targetSize = CGSizeApplyAffineTransform(size, CGAffineTransformMakeScale(scale, scale));
|
||||||
|
imageOptions.resizeMode = PHImageRequestOptionsResizeModeFast;
|
||||||
|
}
|
||||||
|
|
||||||
|
PHImageContentMode contentMode = PHImageContentModeAspectFill;
|
||||||
|
if (resizeMode == RCTResizeModeContain) {
|
||||||
|
contentMode = PHImageContentModeAspectFit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// PHImageRequestID requestID =
|
||||||
|
[[PHImageManager defaultManager] requestImageForAsset:asset
|
||||||
|
targetSize:targetSize
|
||||||
|
contentMode:contentMode
|
||||||
|
options:imageOptions
|
||||||
|
resultHandler:^(UIImage *result, NSDictionary<NSString *, id> *info) {
|
||||||
|
if (result) {
|
||||||
|
|
||||||
|
NSData *imageData = UIImageJPEGRepresentation(result, compression );
|
||||||
|
[imageData writeToFile:destination atomically:YES];
|
||||||
|
resolve(destination);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
NSMutableDictionary* details = [NSMutableDictionary dictionary];
|
||||||
|
[details setValue:info[PHImageErrorKey] forKey:NSLocalizedDescriptionKey];
|
||||||
|
NSError *error = [NSError errorWithDomain:@"RNFS" code:501 userInfo:details];
|
||||||
|
[self reject: reject withError:error];
|
||||||
|
|
||||||
|
}
|
||||||
|
}];
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
- (NSNumber *)dateToTimeIntervalNumber:(NSDate *)date
|
- (NSNumber *)dateToTimeIntervalNumber:(NSDate *)date
|
||||||
{
|
{
|
||||||
return @([date timeIntervalSince1970]);
|
return @([date timeIntervalSince1970]);
|
||||||
|
|
Loading…
Reference in New Issue