mirror of
https://github.com/status-im/react-native.git
synced 2025-01-09 17:15:54 +00:00
914862022c
Summary:Diff D2647083 cleaned up image editing related logics and introduced an image cropping bug. The bug is that the result of the image cropping will be wrong if displaySize is specified. In particular, in Ads Manager App, we generate thumbnail by calling the image cropping function with displaySize set. With this bug, the thumbnail we get is not correct. This diff fixed the bug by replacing `image` with `croppedImage`. It should be a typo from D2647083 Reviewed By: zjj010104 Differential Revision: D2947730 fb-gh-sync-id: df7c7f3ddac5b053425db884f808e27b8418116e shipit-source-id: df7c7f3ddac5b053425db884f808e27b8418116e
83 lines
2.7 KiB
Objective-C
83 lines
2.7 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 "RCTImageEditingManager.h"
|
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
#import "RCTConvert.h"
|
|
#import "RCTLog.h"
|
|
#import "RCTUtils.h"
|
|
#import "RCTImageUtils.h"
|
|
|
|
#import "RCTImageStoreManager.h"
|
|
#import "RCTImageLoader.h"
|
|
|
|
@implementation RCTImageEditingManager
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
/**
|
|
* Crops an image and adds the result to the image store.
|
|
*
|
|
* @param imageTag A URL, a string identifying an asset etc.
|
|
* @param cropData Dictionary with `offset`, `size` and `displaySize`.
|
|
* `offset` and `size` are relative to the full-resolution image size.
|
|
* `displaySize` is an optimization - if specified, the image will
|
|
* be scaled down to `displaySize` rather than `size`.
|
|
* All units are in px (not points).
|
|
*/
|
|
RCT_EXPORT_METHOD(cropImage:(NSString *)imageTag
|
|
cropData:(NSDictionary *)cropData
|
|
successCallback:(RCTResponseSenderBlock)successCallback
|
|
errorCallback:(RCTResponseErrorBlock)errorCallback)
|
|
{
|
|
CGRect rect = {
|
|
[RCTConvert CGPoint:cropData[@"offset"]],
|
|
[RCTConvert CGSize:cropData[@"size"]]
|
|
};
|
|
|
|
[_bridge.imageLoader loadImageWithTag:imageTag callback:^(NSError *error, UIImage *image) {
|
|
if (error) {
|
|
errorCallback(error);
|
|
return;
|
|
}
|
|
|
|
// Crop image
|
|
CGSize targetSize = rect.size;
|
|
CGRect targetRect = {{-rect.origin.x, -rect.origin.y}, image.size};
|
|
CGAffineTransform transform = RCTTransformFromTargetRect(image.size, targetRect);
|
|
UIImage *croppedImage = RCTTransformImage(image, targetSize, image.scale, transform);
|
|
|
|
// Scale image
|
|
if (cropData[@"displaySize"]) {
|
|
targetSize = [RCTConvert CGSize:cropData[@"displaySize"]]; // in pixels
|
|
RCTResizeMode resizeMode = [RCTConvert RCTResizeMode:cropData[@"resizeMode"] ?: @"contain"];
|
|
targetRect = RCTTargetRect(croppedImage.size, targetSize, 1, resizeMode);
|
|
transform = RCTTransformFromTargetRect(croppedImage.size, targetRect);
|
|
croppedImage = RCTTransformImage(croppedImage, targetSize, image.scale, transform);
|
|
}
|
|
|
|
// Store image
|
|
[_bridge.imageStoreManager storeImage:croppedImage withBlock:^(NSString *croppedImageTag) {
|
|
if (!croppedImageTag) {
|
|
NSString *errorMessage = @"Error storing cropped image in RCTImageStoreManager";
|
|
RCTLogWarn(@"%@", errorMessage);
|
|
errorCallback(RCTErrorWithMessage(errorMessage));
|
|
return;
|
|
}
|
|
successCallback(@[croppedImageTag]);
|
|
}];
|
|
}];
|
|
}
|
|
|
|
@end
|