|
12 | 12 | #import "Downloader.h"
|
13 | 13 | #import "Uploader.h"
|
14 | 14 |
|
15 |
| -#if __has_include("RCTEventDispatcher.h") |
16 |
| -#import "RCTEventDispatcher.h" |
17 |
| -#else |
18 | 15 | #import <React/RCTEventDispatcher.h>
|
19 |
| -#endif |
| 16 | +#import <React/RCTUtils.h> |
| 17 | +#import <React/RCTImageLoader.h> |
20 | 18 |
|
21 | 19 | #import <CommonCrypto/CommonDigest.h>
|
| 20 | +#import <Photos/Photos.h> |
| 21 | + |
22 | 22 |
|
23 | 23 | @interface RNFSManager()
|
24 | 24 |
|
@@ -503,6 +503,90 @@ - (dispatch_queue_t)methodQueue
|
503 | 503 | }
|
504 | 504 | }
|
505 | 505 |
|
| 506 | + |
| 507 | +/** |
| 508 | + * iOS Only: copy images from the assets-library (camera-roll) to a specific path, asuming |
| 509 | + * JPEG-Images. Video-Support will follow up, not implemented yet. |
| 510 | + * |
| 511 | + * It is also supported to scale the image via scale-factor (0.0-1.0) or with a specific |
| 512 | + * width and height. Also the resizeMode will be considered. |
| 513 | + */ |
| 514 | +RCT_EXPORT_METHOD(copyAssetsFileIOS: (NSString *) imageUri |
| 515 | + toFilepath: (NSString *) destination |
| 516 | + width: (NSInteger) width |
| 517 | + height: (NSInteger) height |
| 518 | + scale: (CGFloat) scale |
| 519 | + compression: (CGFloat) compression |
| 520 | + resizeMode: (RCTResizeMode) resizeMode |
| 521 | + resolver: (RCTPromiseResolveBlock) resolve |
| 522 | + rejecter: (RCTPromiseRejectBlock) reject) |
| 523 | + |
| 524 | +{ |
| 525 | + CGSize size = CGSizeMake(width, height); |
| 526 | + |
| 527 | + NSURL* url = [NSURL URLWithString:imageUri]; |
| 528 | + PHFetchResult *results = [PHAsset fetchAssetsWithALAssetURLs:@[url] options:nil]; |
| 529 | + |
| 530 | + if (results.count == 0) { |
| 531 | + NSString *errorText = [NSString stringWithFormat:@"Failed to fetch PHAsset with local identifier %@ with no error message.", imageUri]; |
| 532 | + |
| 533 | + NSMutableDictionary* details = [NSMutableDictionary dictionary]; |
| 534 | + [details setValue:errorText forKey:NSLocalizedDescriptionKey]; |
| 535 | + NSError *error = [NSError errorWithDomain:@"RNFS" code:500 userInfo:details]; |
| 536 | + [self reject: reject withError:error]; |
| 537 | + return; |
| 538 | + } |
| 539 | + |
| 540 | + PHAsset *asset = [results firstObject]; |
| 541 | + PHImageRequestOptions *imageOptions = [PHImageRequestOptions new]; |
| 542 | + |
| 543 | + // Allow us to fetch images from iCloud |
| 544 | + imageOptions.networkAccessAllowed = YES; |
| 545 | + |
| 546 | + |
| 547 | + // Note: PhotoKit defaults to a deliveryMode of PHImageRequestOptionsDeliveryModeOpportunistic |
| 548 | + // which means it may call back multiple times - we probably don't want that |
| 549 | + imageOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat; |
| 550 | + |
| 551 | + BOOL useMaximumSize = CGSizeEqualToSize(size, CGSizeZero); |
| 552 | + CGSize targetSize; |
| 553 | + if (useMaximumSize) { |
| 554 | + targetSize = PHImageManagerMaximumSize; |
| 555 | + imageOptions.resizeMode = PHImageRequestOptionsResizeModeNone; |
| 556 | + } else { |
| 557 | + targetSize = CGSizeApplyAffineTransform(size, CGAffineTransformMakeScale(scale, scale)); |
| 558 | + imageOptions.resizeMode = PHImageRequestOptionsResizeModeFast; |
| 559 | + } |
| 560 | + |
| 561 | + PHImageContentMode contentMode = PHImageContentModeAspectFill; |
| 562 | + if (resizeMode == RCTResizeModeContain) { |
| 563 | + contentMode = PHImageContentModeAspectFit; |
| 564 | + } |
| 565 | + |
| 566 | + // PHImageRequestID requestID = |
| 567 | + [[PHImageManager defaultManager] requestImageForAsset:asset |
| 568 | + targetSize:targetSize |
| 569 | + contentMode:contentMode |
| 570 | + options:imageOptions |
| 571 | + resultHandler:^(UIImage *result, NSDictionary<NSString *, id> *info) { |
| 572 | + if (result) { |
| 573 | + |
| 574 | + NSData *imageData = UIImageJPEGRepresentation(result, compression ); |
| 575 | + [imageData writeToFile:destination atomically:YES]; |
| 576 | + resolve(destination); |
| 577 | + |
| 578 | + } else { |
| 579 | + NSMutableDictionary* details = [NSMutableDictionary dictionary]; |
| 580 | + [details setValue:info[PHImageErrorKey] forKey:NSLocalizedDescriptionKey]; |
| 581 | + NSError *error = [NSError errorWithDomain:@"RNFS" code:501 userInfo:details]; |
| 582 | + [self reject: reject withError:error]; |
| 583 | + |
| 584 | + } |
| 585 | + }]; |
| 586 | + |
| 587 | + |
| 588 | +} |
| 589 | + |
506 | 590 | - (NSNumber *)dateToTimeIntervalNumber:(NSDate *)date
|
507 | 591 | {
|
508 | 592 | return @([date timeIntervalSince1970]);
|
|
0 commit comments