2015-09-02 15:25:10 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTPhotoLibraryImageLoader.h"
|
|
|
|
|
|
|
|
#import <Photos/Photos.h>
|
|
|
|
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <React/RCTUtils.h>
|
2015-09-02 15:25:10 +00:00
|
|
|
|
|
|
|
@implementation RCTPhotoLibraryImageLoader
|
|
|
|
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
|
|
|
|
#pragma mark - RCTImageLoader
|
|
|
|
|
|
|
|
- (BOOL)canLoadImageURL:(NSURL *)requestURL
|
|
|
|
{
|
2016-06-30 15:41:47 +00:00
|
|
|
if (![PHAsset class]) {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
return [requestURL.scheme caseInsensitiveCompare:@"assets-library"] == NSOrderedSame ||
|
|
|
|
[requestURL.scheme caseInsensitiveCompare:@"ph"] == NSOrderedSame;
|
2015-09-02 15:25:10 +00:00
|
|
|
}
|
|
|
|
|
2015-10-20 12:00:50 +00:00
|
|
|
- (RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL
|
|
|
|
size:(CGSize)size
|
|
|
|
scale:(CGFloat)scale
|
2016-01-20 19:03:22 +00:00
|
|
|
resizeMode:(RCTResizeMode)resizeMode
|
2015-10-20 12:00:50 +00:00
|
|
|
progressHandler:(RCTImageLoaderProgressBlock)progressHandler
|
2016-09-21 19:11:19 +00:00
|
|
|
partialLoadHandler:(RCTImageLoaderPartialLoadBlock)partialLoadHandler
|
2015-10-20 12:00:50 +00:00
|
|
|
completionHandler:(RCTImageLoaderCompletionBlock)completionHandler
|
2015-09-02 15:25:10 +00:00
|
|
|
{
|
|
|
|
// Using PhotoKit for iOS 8+
|
|
|
|
// The 'ph://' prefix is used by FBMediaKit to differentiate between
|
|
|
|
// assets-library. It is prepended to the local ID so that it is in the
|
|
|
|
// form of an, NSURL which is what assets-library uses.
|
2016-06-30 15:41:47 +00:00
|
|
|
NSString *assetID = @"";
|
|
|
|
PHFetchResult *results;
|
|
|
|
if ([imageURL.scheme caseInsensitiveCompare:@"assets-library"] == NSOrderedSame) {
|
|
|
|
assetID = [imageURL absoluteString];
|
|
|
|
results = [PHAsset fetchAssetsWithALAssetURLs:@[imageURL] options:nil];
|
|
|
|
} else {
|
|
|
|
assetID = [imageURL.absoluteString substringFromIndex:@"ph://".length];
|
|
|
|
results = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetID] options:nil];
|
|
|
|
}
|
2015-09-02 15:25:10 +00:00
|
|
|
if (results.count == 0) {
|
2016-06-30 15:41:47 +00:00
|
|
|
NSString *errorText = [NSString stringWithFormat:@"Failed to fetch PHAsset with local identifier %@ with no error message.", assetID];
|
2015-10-20 12:00:50 +00:00
|
|
|
completionHandler(RCTErrorWithMessage(errorText), nil);
|
2015-09-02 15:25:10 +00:00
|
|
|
return ^{};
|
|
|
|
}
|
|
|
|
|
|
|
|
PHAsset *asset = [results firstObject];
|
|
|
|
PHImageRequestOptions *imageOptions = [PHImageRequestOptions new];
|
2015-10-20 12:00:50 +00:00
|
|
|
|
2016-09-08 23:20:51 +00:00
|
|
|
// Allow PhotoKit to fetch images from iCloud
|
|
|
|
imageOptions.networkAccessAllowed = YES;
|
|
|
|
|
2015-10-20 12:00:50 +00:00
|
|
|
if (progressHandler) {
|
2015-11-14 18:25:00 +00:00
|
|
|
imageOptions.progressHandler = ^(double progress, NSError *error, BOOL *stop, NSDictionary<NSString *, id> *info) {
|
2015-10-20 12:00:50 +00:00
|
|
|
static const double multiplier = 1e6;
|
|
|
|
progressHandler(progress * multiplier, multiplier);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: PhotoKit defaults to a deliveryMode of PHImageRequestOptionsDeliveryModeOpportunistic
|
|
|
|
// which means it may call back multiple times - we probably don't want that
|
2015-10-21 19:43:23 +00:00
|
|
|
imageOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
|
2015-09-02 15:25:10 +00:00
|
|
|
|
|
|
|
BOOL useMaximumSize = CGSizeEqualToSize(size, CGSizeZero);
|
|
|
|
CGSize targetSize;
|
|
|
|
if (useMaximumSize) {
|
|
|
|
targetSize = PHImageManagerMaximumSize;
|
|
|
|
imageOptions.resizeMode = PHImageRequestOptionsResizeModeNone;
|
|
|
|
} else {
|
2016-06-28 07:29:13 +00:00
|
|
|
targetSize = CGSizeApplyAffineTransform(size, CGAffineTransformMakeScale(scale, scale));
|
2015-09-02 15:25:10 +00:00
|
|
|
imageOptions.resizeMode = PHImageRequestOptionsResizeModeFast;
|
|
|
|
}
|
|
|
|
|
|
|
|
PHImageContentMode contentMode = PHImageContentModeAspectFill;
|
2016-01-20 19:03:22 +00:00
|
|
|
if (resizeMode == RCTResizeModeContain) {
|
2015-09-02 15:25:10 +00:00
|
|
|
contentMode = PHImageContentModeAspectFit;
|
|
|
|
}
|
|
|
|
|
2015-10-20 12:00:50 +00:00
|
|
|
PHImageRequestID requestID =
|
|
|
|
[[PHImageManager defaultManager] requestImageForAsset:asset
|
|
|
|
targetSize:targetSize
|
|
|
|
contentMode:contentMode
|
|
|
|
options:imageOptions
|
2015-11-14 18:25:00 +00:00
|
|
|
resultHandler:^(UIImage *result, NSDictionary<NSString *, id> *info) {
|
2015-09-02 15:25:10 +00:00
|
|
|
if (result) {
|
|
|
|
completionHandler(nil, result);
|
|
|
|
} else {
|
|
|
|
completionHandler(info[PHImageErrorKey], nil);
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
return ^{
|
|
|
|
[[PHImageManager defaultManager] cancelImageRequest:requestID];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|