RCTPhotoLibraryImageLoader.mm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import "RCTPhotoLibraryImageLoader.h"
  8. #import <Photos/Photos.h>
  9. #import <React/RCTLog.h>
  10. #import <React/RCTUtils.h>
  11. #import <ReactCommon/RCTTurboModule.h>
  12. #import "RCTCameraRollPlugins.h"
  13. @interface RCTPhotoLibraryImageLoader () <RCTTurboModule>
  14. @end
  15. @implementation RCTPhotoLibraryImageLoader
  16. RCT_EXPORT_MODULE()
  17. @synthesize bridge = _bridge;
  18. #pragma mark - RCTImageLoader
  19. - (BOOL)canLoadImageURL:(NSURL *)requestURL
  20. {
  21. if (![PHAsset class]) {
  22. return NO;
  23. }
  24. return [requestURL.scheme caseInsensitiveCompare:@"assets-library"] == NSOrderedSame ||
  25. [requestURL.scheme caseInsensitiveCompare:@"ph"] == NSOrderedSame;
  26. }
  27. - (RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL
  28. size:(CGSize)size
  29. scale:(CGFloat)scale
  30. resizeMode:(RCTResizeMode)resizeMode
  31. progressHandler:(RCTImageLoaderProgressBlock)progressHandler
  32. partialLoadHandler:(RCTImageLoaderPartialLoadBlock)partialLoadHandler
  33. completionHandler:(RCTImageLoaderCompletionBlock)completionHandler
  34. {
  35. // Using PhotoKit for iOS 8+
  36. // The 'ph://' prefix is used by FBMediaKit to differentiate between
  37. // assets-library. It is prepended to the local ID so that it is in the
  38. // form of an NSURL which is what assets-library uses.
  39. NSString *assetID = @"";
  40. PHFetchResult *results;
  41. if (!imageURL) {
  42. completionHandler(RCTErrorWithMessage(@"Cannot load a photo library asset with no URL"), nil);
  43. return ^{};
  44. } else if ([imageURL.scheme caseInsensitiveCompare:@"assets-library"] == NSOrderedSame) {
  45. assetID = [imageURL absoluteString];
  46. results = [PHAsset fetchAssetsWithALAssetURLs:@[imageURL] options:nil];
  47. } else {
  48. assetID = [imageURL.absoluteString substringFromIndex:@"ph://".length];
  49. results = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetID] options:nil];
  50. }
  51. if (results.count == 0) {
  52. NSString *errorText = [NSString stringWithFormat:@"Failed to fetch PHAsset with local identifier %@ with no error message.", assetID];
  53. completionHandler(RCTErrorWithMessage(errorText), nil);
  54. return ^{};
  55. }
  56. PHAsset *asset = [results firstObject];
  57. PHImageRequestOptions *imageOptions = [PHImageRequestOptions new];
  58. // Allow PhotoKit to fetch images from iCloud
  59. imageOptions.networkAccessAllowed = YES;
  60. if (progressHandler) {
  61. imageOptions.progressHandler = ^(double progress, NSError *error, BOOL *stop, NSDictionary<NSString *, id> *info) {
  62. static const double multiplier = 1e6;
  63. progressHandler(progress * multiplier, multiplier);
  64. };
  65. }
  66. // Note: PhotoKit defaults to a deliveryMode of PHImageRequestOptionsDeliveryModeOpportunistic
  67. // which means it may call back multiple times - we probably don't want that
  68. imageOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
  69. BOOL useMaximumSize = CGSizeEqualToSize(size, CGSizeZero);
  70. CGSize targetSize;
  71. if (useMaximumSize) {
  72. targetSize = PHImageManagerMaximumSize;
  73. imageOptions.resizeMode = PHImageRequestOptionsResizeModeNone;
  74. } else {
  75. targetSize = CGSizeApplyAffineTransform(size, CGAffineTransformMakeScale(scale, scale));
  76. imageOptions.resizeMode = PHImageRequestOptionsResizeModeFast;
  77. }
  78. PHImageContentMode contentMode = PHImageContentModeAspectFill;
  79. if (resizeMode == RCTResizeModeContain) {
  80. contentMode = PHImageContentModeAspectFit;
  81. }
  82. PHImageRequestID requestID =
  83. [[PHImageManager defaultManager] requestImageForAsset:asset
  84. targetSize:targetSize
  85. contentMode:contentMode
  86. options:imageOptions
  87. resultHandler:^(UIImage *result, NSDictionary<NSString *, id> *info) {
  88. if (result) {
  89. completionHandler(nil, result);
  90. } else {
  91. completionHandler(info[PHImageErrorKey], nil);
  92. }
  93. }];
  94. return ^{
  95. [[PHImageManager defaultManager] cancelImageRequest:requestID];
  96. };
  97. }
  98. @end
  99. Class RCTPhotoLibraryImageLoaderCls(void) {
  100. return RCTPhotoLibraryImageLoader.class;
  101. }