mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-26 23:05:00 +08:00
[React Native] open source ImageEditingManager native module
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
143879351AAD238D00F088A5 /* RCTCameraRollManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 143879341AAD238D00F088A5 /* RCTCameraRollManager.m */; };
|
||||
143879381AAD32A300F088A5 /* RCTImageLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = 143879371AAD32A300F088A5 /* RCTImageLoader.m */; };
|
||||
35123E6B1B59C99D00EBAD80 /* RCTImageStoreManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 35123E6A1B59C99D00EBAD80 /* RCTImageStoreManager.m */; };
|
||||
354631681B69857700AA0B86 /* RCTImageEditingManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 354631671B69857700AA0B86 /* RCTImageEditingManager.m */; };
|
||||
58B5118F1A9E6BD600147676 /* RCTImageDownloader.m in Sources */ = {isa = PBXBuildFile; fileRef = 58B5118A1A9E6BD600147676 /* RCTImageDownloader.m */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
@@ -50,6 +51,8 @@
|
||||
143879371AAD32A300F088A5 /* RCTImageLoader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTImageLoader.m; sourceTree = "<group>"; };
|
||||
35123E691B59C99D00EBAD80 /* RCTImageStoreManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTImageStoreManager.h; sourceTree = "<group>"; };
|
||||
35123E6A1B59C99D00EBAD80 /* RCTImageStoreManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTImageStoreManager.m; sourceTree = "<group>"; };
|
||||
354631661B69857700AA0B86 /* RCTImageEditingManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTImageEditingManager.h; sourceTree = "<group>"; };
|
||||
354631671B69857700AA0B86 /* RCTImageEditingManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTImageEditingManager.m; sourceTree = "<group>"; };
|
||||
58B5115D1A9E6B3D00147676 /* libRCTImage.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRCTImage.a; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
58B511891A9E6BD600147676 /* RCTImageDownloader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTImageDownloader.h; sourceTree = "<group>"; };
|
||||
58B5118A1A9E6BD600147676 /* RCTImageDownloader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTImageDownloader.m; sourceTree = "<group>"; };
|
||||
@@ -75,6 +78,8 @@
|
||||
1304D5B11AA8C50D0002E2BE /* RCTGIFImage.m */,
|
||||
58B511891A9E6BD600147676 /* RCTImageDownloader.h */,
|
||||
58B5118A1A9E6BD600147676 /* RCTImageDownloader.m */,
|
||||
354631661B69857700AA0B86 /* RCTImageEditingManager.h */,
|
||||
354631671B69857700AA0B86 /* RCTImageEditingManager.m */,
|
||||
143879361AAD32A300F088A5 /* RCTImageLoader.h */,
|
||||
143879371AAD32A300F088A5 /* RCTImageLoader.m */,
|
||||
137620331B31C53500677FF0 /* RCTImagePickerManager.h */,
|
||||
@@ -167,6 +172,7 @@
|
||||
1304D5B21AA8C50D0002E2BE /* RCTGIFImage.m in Sources */,
|
||||
143879351AAD238D00F088A5 /* RCTCameraRollManager.m in Sources */,
|
||||
143879381AAD32A300F088A5 /* RCTImageLoader.m in Sources */,
|
||||
354631681B69857700AA0B86 /* RCTImageEditingManager.m in Sources */,
|
||||
1304D5AB1AA8C4A30002E2BE /* RCTImageView.m in Sources */,
|
||||
134B00A21B54232B00EC8DFB /* RCTImageUtils.m in Sources */,
|
||||
);
|
||||
|
||||
14
Libraries/Image/RCTImageEditingManager.h
Normal file
14
Libraries/Image/RCTImageEditingManager.h
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* 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 "RCTBridgeModule.h"
|
||||
|
||||
@interface RCTImageEditingManager : NSObject <RCTBridgeModule>
|
||||
|
||||
@end
|
||||
133
Libraries/Image/RCTImageEditingManager.m
Normal file
133
Libraries/Image/RCTImageEditingManager.m
Normal file
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
* 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 "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)
|
||||
{
|
||||
NSDictionary *offset = cropData[@"offset"];
|
||||
NSDictionary *size = cropData[@"size"];
|
||||
NSDictionary *displaySize = cropData[@"displaySize"];
|
||||
NSString *resizeMode = cropData[@"resizeMode"] ?: @"contain";
|
||||
|
||||
if (!offset[@"x"] || !offset[@"y"] || !size[@"width"] || !size[@"height"]) {
|
||||
NSString *errorMessage = [NSString stringWithFormat:@"Invalid cropData: %@", cropData];
|
||||
RCTLogError(@"%@", errorMessage);
|
||||
errorCallback(RCTErrorWithMessage(errorMessage));
|
||||
return;
|
||||
}
|
||||
|
||||
[_bridge.imageLoader loadImageWithTag:imageTag callback:^(NSError *error, UIImage *image) {
|
||||
if (error) {
|
||||
errorCallback(error);
|
||||
return;
|
||||
}
|
||||
CGRect rect = (CGRect){
|
||||
[RCTConvert CGPoint:offset],
|
||||
[RCTConvert CGSize:size]
|
||||
};
|
||||
|
||||
// Crop image
|
||||
CGRect rectToDrawIn = {{-rect.origin.x, -rect.origin.y}, image.size};
|
||||
UIGraphicsBeginImageContextWithOptions(rect.size, !RCTImageHasAlpha(image.CGImage), image.scale);
|
||||
[image drawInRect:rectToDrawIn];
|
||||
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
|
||||
UIGraphicsEndImageContext();
|
||||
|
||||
if (displaySize && displaySize[@"width"] && displaySize[@"height"]) {
|
||||
CGSize targetSize = [RCTConvert CGSize:displaySize];
|
||||
croppedImage = [self scaleImage:croppedImage targetSize:targetSize resizeMode:resizeMode];
|
||||
}
|
||||
|
||||
[_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]);
|
||||
}];
|
||||
}];
|
||||
}
|
||||
|
||||
- (UIImage *)scaleImage:(UIImage *)image targetSize:(CGSize)targetSize resizeMode:(NSString *)resizeMode
|
||||
{
|
||||
if (CGSizeEqualToSize(image.size, targetSize)) {
|
||||
return image;
|
||||
}
|
||||
|
||||
CGFloat imageRatio = image.size.width / image.size.height;
|
||||
CGFloat targetRatio = targetSize.width / targetSize.height;
|
||||
|
||||
CGFloat newWidth = targetSize.width;
|
||||
CGFloat newHeight = targetSize.height;
|
||||
|
||||
// contain vs cover
|
||||
// http://blog.vjeux.com/2013/image/css-container-and-cover.html
|
||||
if ([resizeMode isEqualToString:@"contain"]) {
|
||||
if (imageRatio <= targetRatio) {
|
||||
newWidth = targetSize.height * imageRatio;
|
||||
newHeight = targetSize.height;
|
||||
} else {
|
||||
newWidth = targetSize.width;
|
||||
newHeight = targetSize.width / imageRatio;
|
||||
}
|
||||
} else if ([resizeMode isEqualToString:@"cover"]) {
|
||||
if (imageRatio <= targetRatio) {
|
||||
newWidth = targetSize.width;
|
||||
newHeight = targetSize.width / imageRatio;
|
||||
} else {
|
||||
newWidth = targetSize.height * imageRatio;
|
||||
newHeight = targetSize.height;
|
||||
}
|
||||
} // else assume we're stretching the image
|
||||
|
||||
// prevent upscaling
|
||||
newWidth = MIN(newWidth, image.size.width);
|
||||
newHeight = MIN(newHeight, image.size.height);
|
||||
|
||||
// perform the scaling @1x because targetSize is in actual pixel width/height
|
||||
UIGraphicsBeginImageContextWithOptions(targetSize, NO, 1.0f);
|
||||
[image drawInRect:CGRectMake(0.f, 0.f, newWidth, newHeight)];
|
||||
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
|
||||
UIGraphicsEndImageContext();
|
||||
|
||||
return scaledImage;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -61,7 +61,7 @@ RCT_EXPORT_MODULE()
|
||||
{
|
||||
return [self loadImageWithTag:imageTag
|
||||
size:CGSizeZero
|
||||
scale:0
|
||||
scale:1
|
||||
resizeMode:UIViewContentModeScaleToFill
|
||||
progressBlock:nil
|
||||
completionBlock:callback];
|
||||
|
||||
Reference in New Issue
Block a user