mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
d45d4eafa8
Reviewed By: fkgozali Differential Revision: D30280757 fbshipit-source-id: 57385d3fd64f564f1de9ad86ffb2c0064e941df9
101 lines
3.6 KiB
Plaintext
101 lines
3.6 KiB
Plaintext
/*
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#import "RCTSyncImageManager.h"
|
|
|
|
#import <react/utils/ManagedObjectWrapper.h>
|
|
#import <react/utils/SharedFunction.h>
|
|
|
|
#import <React/RCTAssert.h>
|
|
#import <React/RCTImageLoaderWithAttributionProtocol.h>
|
|
#import <React/RCTLog.h>
|
|
#import <react/renderer/imagemanager/ImageResponse.h>
|
|
#import <react/renderer/imagemanager/ImageResponseObserver.h>
|
|
|
|
#import "RCTImagePrimitivesConversions.h"
|
|
|
|
using namespace facebook::react;
|
|
|
|
@implementation RCTSyncImageManager {
|
|
id<RCTImageLoaderWithAttributionProtocol> _imageLoader;
|
|
}
|
|
|
|
- (instancetype)initWithImageLoader:(id<RCTImageLoaderWithAttributionProtocol>)imageLoader
|
|
{
|
|
if (self = [super init]) {
|
|
RCTAssert(RCTRunningInTestEnvironment(), @"This class is only meant to be used in test environment");
|
|
_imageLoader = imageLoader;
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
- (ImageRequest)requestImage:(ImageSource)imageSource surfaceId:(SurfaceId)surfaceId
|
|
{
|
|
auto telemetry = std::make_shared<ImageTelemetry>(surfaceId);
|
|
auto imageRequest = ImageRequest(imageSource, telemetry);
|
|
auto weakObserverCoordinator =
|
|
(std::weak_ptr<const ImageResponseObserverCoordinator>)imageRequest.getSharedObserverCoordinator();
|
|
|
|
auto sharedCancelationFunction = SharedFunction<>();
|
|
imageRequest.setCancelationFunction(sharedCancelationFunction);
|
|
|
|
dispatch_group_t imageWaitGroup = dispatch_group_create();
|
|
|
|
dispatch_group_enter(imageWaitGroup);
|
|
|
|
NSURLRequest *request = NSURLRequestFromImageSource(imageSource);
|
|
|
|
auto completionBlock = ^(NSError *error, UIImage *image, id metadata) {
|
|
auto observerCoordinator = weakObserverCoordinator.lock();
|
|
if (!observerCoordinator) {
|
|
return;
|
|
}
|
|
|
|
if (image && !error) {
|
|
auto wrappedMetadata = metadata ? wrapManagedObject(metadata) : nullptr;
|
|
observerCoordinator->nativeImageResponseComplete(ImageResponse(wrapManagedObject(image), wrappedMetadata));
|
|
} else {
|
|
observerCoordinator->nativeImageResponseFailed();
|
|
}
|
|
dispatch_group_leave(imageWaitGroup);
|
|
};
|
|
|
|
auto progressBlock = ^(int64_t progress, int64_t total) {
|
|
auto observerCoordinator = weakObserverCoordinator.lock();
|
|
if (!observerCoordinator) {
|
|
return;
|
|
}
|
|
|
|
observerCoordinator->nativeImageResponseProgress(progress / (float)total);
|
|
};
|
|
|
|
RCTImageURLLoaderRequest *loaderRequest =
|
|
[self->_imageLoader loadImageWithURLRequest:request
|
|
size:CGSizeMake(imageSource.size.width, imageSource.size.height)
|
|
scale:imageSource.scale
|
|
clipped:YES
|
|
resizeMode:RCTResizeModeStretch
|
|
priority:RCTImageLoaderPriorityImmediate
|
|
attribution:{
|
|
.surfaceId = surfaceId,
|
|
}
|
|
progressBlock:progressBlock
|
|
partialLoadBlock:nil
|
|
completionBlock:completionBlock];
|
|
RCTImageLoaderCancellationBlock cancelationBlock = loaderRequest.cancellationBlock;
|
|
sharedCancelationFunction.assign([cancelationBlock]() { cancelationBlock(); });
|
|
|
|
auto result = dispatch_group_wait(imageWaitGroup, dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC));
|
|
if (result != 0) {
|
|
RCTLogError(@"Image timed out in test environment for url: %@", loaderRequest.imageURL);
|
|
}
|
|
return imageRequest;
|
|
}
|
|
|
|
@end
|