mirror of
https://github.com/status-im/react-native.git
synced 2025-01-10 09:35:48 +00:00
e1577df1fd
Summary: To make React Native play nicely with our internal build infrastructure we need to properly namespace all of our header includes. Where previously you could do `#import "RCTBridge.h"`, you must now write this as `#import <React/RCTBridge.h>`. If your xcode project still has a custom header include path, both variants will likely continue to work, but for new projects, we're defaulting the header include path to `$(BUILT_PRODUCTS_DIR)/usr/local/include`, where the React and CSSLayout targets will copy a subset of headers too. To make Xcode copy headers phase work properly, you may need to add React as an explicit dependency to your app's scheme and disable "parallelize build". Reviewed By: mmmulani Differential Revision: D4213120 fbshipit-source-id: 84a32a4b250c27699e6795f43584f13d594a9a82
111 lines
4.1 KiB
Objective-C
111 lines
4.1 KiB
Objective-C
/**
|
|
* 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>
|
|
|
|
#import <React/RCTUtils.h>
|
|
|
|
@implementation RCTPhotoLibraryImageLoader
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
#pragma mark - RCTImageLoader
|
|
|
|
- (BOOL)canLoadImageURL:(NSURL *)requestURL
|
|
{
|
|
if (![PHAsset class]) {
|
|
return NO;
|
|
}
|
|
return [requestURL.scheme caseInsensitiveCompare:@"assets-library"] == NSOrderedSame ||
|
|
[requestURL.scheme caseInsensitiveCompare:@"ph"] == NSOrderedSame;
|
|
}
|
|
|
|
- (RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL
|
|
size:(CGSize)size
|
|
scale:(CGFloat)scale
|
|
resizeMode:(RCTResizeMode)resizeMode
|
|
progressHandler:(RCTImageLoaderProgressBlock)progressHandler
|
|
partialLoadHandler:(RCTImageLoaderPartialLoadBlock)partialLoadHandler
|
|
completionHandler:(RCTImageLoaderCompletionBlock)completionHandler
|
|
{
|
|
// 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.
|
|
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];
|
|
}
|
|
if (results.count == 0) {
|
|
NSString *errorText = [NSString stringWithFormat:@"Failed to fetch PHAsset with local identifier %@ with no error message.", assetID];
|
|
completionHandler(RCTErrorWithMessage(errorText), nil);
|
|
return ^{};
|
|
}
|
|
|
|
PHAsset *asset = [results firstObject];
|
|
PHImageRequestOptions *imageOptions = [PHImageRequestOptions new];
|
|
|
|
// Allow PhotoKit to fetch images from iCloud
|
|
imageOptions.networkAccessAllowed = YES;
|
|
|
|
if (progressHandler) {
|
|
imageOptions.progressHandler = ^(double progress, NSError *error, BOOL *stop, NSDictionary<NSString *, id> *info) {
|
|
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
|
|
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) {
|
|
completionHandler(nil, result);
|
|
} else {
|
|
completionHandler(info[PHImageErrorKey], nil);
|
|
}
|
|
}];
|
|
|
|
return ^{
|
|
[[PHImageManager defaultManager] cancelImageRequest:requestID];
|
|
};
|
|
}
|
|
|
|
@end
|