mirror of
https://github.com/status-im/react-native-image-crop-picker.git
synced 2025-02-23 10:58:16 +00:00
Return cropped image rectangle (#458)
* android: Update ucrop version #457 Use latest uCrop version. * Return cropped image rectangle #457 * Update README.md #457 Update Android installation instructions.
This commit is contained in:
parent
1476fd80aa
commit
5633a99c4a
10
README.md
10
README.md
@ -202,6 +202,16 @@ buildscript {
|
||||
}
|
||||
```
|
||||
|
||||
- Add the following to your `build.gradle`'s repositories section. (project build.gradle)
|
||||
```gradle
|
||||
allprojects {
|
||||
repositories {
|
||||
jcenter()
|
||||
maven { url "https://jitpack.io" }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- Add `useSupportLibrary` (app build.gradle)
|
||||
|
||||
```gradle
|
||||
|
@ -16,6 +16,6 @@ android {
|
||||
|
||||
dependencies {
|
||||
compile 'com.facebook.react:react-native:+'
|
||||
compile 'com.yalantis:ucrop:2.2.0-native'
|
||||
compile 'com.github.yalantis:ucrop:2.2.1-native'
|
||||
compile 'id.zelory:compressor:2.1.0'
|
||||
}
|
||||
|
@ -678,8 +678,11 @@ class PickerModule extends ReactContextBaseJavaModule implements ActivityEventLi
|
||||
final Uri resultUri = UCrop.getOutput(data);
|
||||
if (resultUri != null) {
|
||||
try {
|
||||
WritableMap result = getSelection(activity, resultUri, false);
|
||||
result.putMap("cropRect", PickerModule.getCroppedRectMap(data));
|
||||
|
||||
resultCollector.setWaitCount(1);
|
||||
resultCollector.notifySuccess(getSelection(activity, resultUri, false));
|
||||
resultCollector.notifySuccess(result);
|
||||
} catch (Exception ex) {
|
||||
resultCollector.notifyProblem(E_NO_IMAGE_DATA_FOUND, ex.getMessage());
|
||||
}
|
||||
@ -729,4 +732,16 @@ class PickerModule extends ReactContextBaseJavaModule implements ActivityEventLi
|
||||
return image;
|
||||
|
||||
}
|
||||
|
||||
private static WritableMap getCroppedRectMap(Intent data) {
|
||||
final int DEFAULT_VALUE = -1;
|
||||
final WritableMap map = new WritableNativeMap();
|
||||
|
||||
map.putInt("x", data.getIntExtra(UCrop.EXTRA_OUTPUT_OFFSET_X, DEFAULT_VALUE));
|
||||
map.putInt("y", data.getIntExtra(UCrop.EXTRA_OUTPUT_OFFSET_Y, DEFAULT_VALUE));
|
||||
map.putInt("width", data.getIntExtra(UCrop.EXTRA_OUTPUT_IMAGE_WIDTH, DEFAULT_VALUE));
|
||||
map.putInt("height", data.getIntExtra(UCrop.EXTRA_OUTPUT_IMAGE_HEIGHT, DEFAULT_VALUE));
|
||||
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
8
index.d.ts
vendored
8
index.d.ts
vendored
@ -33,6 +33,14 @@ declare module "react-native-image-crop-picker" {
|
||||
height: number;
|
||||
mime: string;
|
||||
exif: null | object;
|
||||
cropRect: null | CropRect
|
||||
}
|
||||
|
||||
export interface CropRect {
|
||||
x: number;
|
||||
y: number;
|
||||
width: number;
|
||||
height: number;
|
||||
}
|
||||
|
||||
export function openPicker(options: Options): Promise<Image | Image[]>;
|
||||
|
@ -400,6 +400,7 @@ RCT_EXPORT_METHOD(openCropper:(NSDictionary *)options
|
||||
withMime:@"video/mp4"
|
||||
withSize:fileSizeValue
|
||||
withData:nil
|
||||
withRect:CGRectNull
|
||||
withCreationDate:forAsset.creationDate
|
||||
withModificationDate:forAsset.modificationDate
|
||||
]);
|
||||
@ -410,8 +411,7 @@ RCT_EXPORT_METHOD(openCropper:(NSDictionary *)options
|
||||
}];
|
||||
}
|
||||
|
||||
- (NSDictionary*) createAttachmentResponse:(NSString*)filePath withExif:(NSDictionary*) exif withSourceURL:(NSString*)sourceURL withLocalIdentifier:(NSString*)localIdentifier withFilename:(NSString*)filename withWidth:(NSNumber*)width withHeight:(NSNumber*)height withMime:(NSString*)mime withSize:(NSNumber*)size withData:(NSString*)data withCreationDate:(NSDate*)creationDate withModificationDate:(NSDate*)modificationDate
|
||||
{
|
||||
- (NSDictionary*) createAttachmentResponse:(NSString*)filePath withExif:(NSDictionary*) exif withSourceURL:(NSString*)sourceURL withLocalIdentifier:(NSString*)localIdentifier withFilename:(NSString*)filename withWidth:(NSNumber*)width withHeight:(NSNumber*)height withMime:(NSString*)mime withSize:(NSNumber*)size withData:(NSString*)data withRect:(CGRect)cropRect withCreationDate:(NSDate*)creationDate withModificationDate:(NSDate*)modificationDate {
|
||||
return @{
|
||||
@"path": filePath,
|
||||
@"sourceURL": (sourceURL) ? sourceURL : [NSNull null],
|
||||
@ -423,6 +423,7 @@ RCT_EXPORT_METHOD(openCropper:(NSDictionary *)options
|
||||
@"size": size,
|
||||
@"data": (data) ? data : [NSNull null],
|
||||
@"exif": (exif) ? exif : [NSNull null],
|
||||
@"cropRect": CGRectIsNull(cropRect) ? [NSNull null] : [ImageCropPicker cgRectToDictionary:cropRect],
|
||||
@"creationDate:": (creationDate) ? [NSString stringWithFormat:@"%.0f", [creationDate timeIntervalSince1970]] : [NSNull null],
|
||||
@"modificationDate": (modificationDate) ? [NSString stringWithFormat:@"%.0f", [modificationDate timeIntervalSince1970]] : [NSNull null],
|
||||
};
|
||||
@ -515,6 +516,7 @@ RCT_EXPORT_METHOD(openCropper:(NSDictionary *)options
|
||||
withMime:imageResult.mime
|
||||
withSize:[NSNumber numberWithUnsignedInteger:imageResult.data.length]
|
||||
withData:[[self.options objectForKey:@"includeBase64"] boolValue] ? [imageResult.data base64EncodedStringWithOptions:0]: nil
|
||||
withRect:CGRectNull
|
||||
withCreationDate:phAsset.creationDate
|
||||
withModificationDate:phAsset.modificationDate
|
||||
]];
|
||||
@ -638,9 +640,11 @@ RCT_EXPORT_METHOD(openCropper:(NSDictionary *)options
|
||||
withHeight:imageResult.height
|
||||
withMime:imageResult.mime
|
||||
withSize:[NSNumber numberWithUnsignedInteger:imageResult.data.length]
|
||||
withData:[[self.options objectForKey:@"includeBase64"] boolValue] ? [imageResult.data base64EncodedStringWithOptions:0] : nil
|
||||
withData:[[self.options objectForKey:@"includeBase64"] boolValue] ? [imageResult.data base64EncodedStringWithOptions:0] : nil
|
||||
withRect:CGRectNull
|
||||
withCreationDate:creationDate
|
||||
withModificationDate:modificationDate]);
|
||||
withModificationDate:modificationDate
|
||||
]);
|
||||
}]];
|
||||
}
|
||||
}
|
||||
@ -763,8 +767,10 @@ RCT_EXPORT_METHOD(openCropper:(NSDictionary *)options
|
||||
withMime:imageResult.mime
|
||||
withSize:[NSNumber numberWithUnsignedInteger:imageResult.data.length]
|
||||
withData:[[self.options objectForKey:@"includeBase64"] boolValue] ? [imageResult.data base64EncodedStringWithOptions:0] : nil
|
||||
withRect:cropRect
|
||||
withCreationDate:self.croppingFile[@"creationDate"]
|
||||
withModificationDate:self.croppingFile[@"modificationDate"]]);
|
||||
withModificationDate:self.croppingFile[@"modificationDate"]
|
||||
]);
|
||||
}]];
|
||||
}
|
||||
|
||||
@ -794,4 +800,15 @@ RCT_EXPORT_METHOD(openCropper:(NSDictionary *)options
|
||||
[self imageCropViewController:controller didCropImage:croppedImage usingCropRect:cropRect];
|
||||
}
|
||||
|
||||
|
||||
|
||||
+ (NSDictionary *)cgRectToDictionary:(CGRect)rect {
|
||||
return @{
|
||||
@"x": [NSNumber numberWithFloat: rect.origin.x],
|
||||
@"y": [NSNumber numberWithFloat: rect.origin.y],
|
||||
@"width": [NSNumber numberWithFloat: CGRectGetWidth(rect)],
|
||||
@"height": [NSNumber numberWithFloat: CGRectGetHeight(rect)]
|
||||
};
|
||||
}
|
||||
|
||||
@end
|
Loading…
x
Reference in New Issue
Block a user