mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-05-13 20:55:32 +08:00
Summary: Part of: #23313. This moves the `RCTTest` lib from `Libraries/RCTTest` to `RNTester/RCTTest`. This also removes `takeSnapshot` from React Native, and implements it as a standalone module in RNTester called `ScreenshotManager`. [General] [Removed] - RCTTest & ReactNative.takeSnapshot Pull Request resolved: https://github.com/facebook/react-native/pull/23721 Differential Revision: D14434796 Pulled By: PeteTheHeat fbshipit-source-id: d6e103a0ea0b6702701cdb5ce8449163ca4628ce
85 lines
2.5 KiB
Objective-C
85 lines
2.5 KiB
Objective-C
//
|
|
// RNTTakeScreenshot.m
|
|
// RNTester
|
|
//
|
|
// Created by Eric Lewis on 3/1/19.
|
|
// Copyright © 2019 Facebook. All rights reserved.
|
|
//
|
|
|
|
#import "Screenshot.h"
|
|
|
|
#import <React/RCTUIManager.h>
|
|
|
|
@implementation ScreenshotManager
|
|
|
|
RCT_EXPORT_MODULE();
|
|
|
|
RCT_EXPORT_METHOD(takeScreenshot:(id /* NSString or NSNumber */)target
|
|
withOptions:(NSDictionary *)options
|
|
resolve:(RCTPromiseResolveBlock)resolve
|
|
reject:(RCTPromiseRejectBlock)reject)
|
|
{
|
|
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
|
|
|
|
// Get view
|
|
UIView *view;
|
|
if (target == nil || [target isEqual:@"window"]) {
|
|
view = RCTKeyWindow();
|
|
} else if ([target isKindOfClass:[NSNumber class]]) {
|
|
view = viewRegistry[target];
|
|
if (!view) {
|
|
RCTLogError(@"No view found with reactTag: %@", target);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Get options
|
|
CGSize size = [RCTConvert CGSize:options];
|
|
NSString *format = [RCTConvert NSString:options[@"format"] ?: @"png"];
|
|
|
|
// Capture image
|
|
if (size.width < 0.1 || size.height < 0.1) {
|
|
size = view.bounds.size;
|
|
}
|
|
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
|
|
BOOL success = [view drawViewHierarchyInRect:(CGRect){CGPointZero, size} afterScreenUpdates:YES];
|
|
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
|
|
UIGraphicsEndImageContext();
|
|
|
|
if (!success || !image) {
|
|
reject(RCTErrorUnspecified, @"Failed to capture view snapshot.", nil);
|
|
return;
|
|
}
|
|
|
|
// Convert image to data (on a background thread)
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
|
|
NSData *data;
|
|
if ([format isEqualToString:@"png"]) {
|
|
data = UIImagePNGRepresentation(image);
|
|
} else if ([format isEqualToString:@"jpeg"]) {
|
|
CGFloat quality = [RCTConvert CGFloat:options[@"quality"] ?: @1];
|
|
data = UIImageJPEGRepresentation(image, quality);
|
|
} else {
|
|
RCTLogError(@"Unsupported image format: %@", format);
|
|
return;
|
|
}
|
|
|
|
// Save to a temp file
|
|
NSError *error = nil;
|
|
NSString *tempFilePath = RCTTempFilePath(format, &error);
|
|
if (tempFilePath) {
|
|
if ([data writeToFile:tempFilePath options:(NSDataWritingOptions)0 error:&error]) {
|
|
resolve(tempFilePath);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// If we reached here, something went wrong
|
|
reject(RCTErrorUnspecified, error.localizedDescription, error);
|
|
});
|
|
}];
|
|
}
|
|
|
|
@end
|