mirror of
https://github.com/apache/cordova-plugin-camera.git
synced 2026-02-25 00:00:06 +08:00
Compare commits
2 Commits
master
...
pr-ios-ren
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7954401d8d | ||
|
|
6f77735f79 |
@@ -421,17 +421,37 @@ static NSString* MIME_JPEG = @"image/jpeg";
|
||||
}
|
||||
|
||||
/**
|
||||
Prepares the image and metadata obtained from PHPickerImageViewController which will be processed in
|
||||
resultForImage:. After that CDVPluginResult is returned.
|
||||
Processes an image obtained from PHPickerViewController according to specified pictureOptions,
|
||||
after it returns the CDVPluginResult.
|
||||
The processing of the image is similar what retrieveImage: and processImage: is doing for UIImagePickerController.
|
||||
*/
|
||||
- (void)processPHPickerImage:(UIImage*)image
|
||||
metadata:(NSDictionary*)metadata
|
||||
callbackId:(NSString*)callbackId
|
||||
options:(CDVPictureOptions*)options API_AVAILABLE(ios(14))
|
||||
{
|
||||
// To shrink the file size, only selected meta data like EXIF, TIFF, and GPS is used,
|
||||
// which will be stored in self.metadata, which is set to the image in resultForImage:
|
||||
// This code replicates the logic from processImage: for the UIImagePickerController
|
||||
// Process image according to options
|
||||
// The same is done in retrieveImage:
|
||||
UIImage *processedImage = image;
|
||||
|
||||
if (options.correctOrientation) {
|
||||
processedImage = [processedImage imageCorrectedForCaptureOrientation];
|
||||
}
|
||||
|
||||
// Scale with optional cropping
|
||||
if ((options.targetSize.width > 0) && (options.targetSize.height > 0)) {
|
||||
// Scale and crop to target size
|
||||
if (options.cropToSize) {
|
||||
processedImage = [processedImage imageByScalingAndCroppingForSize:options.targetSize];
|
||||
|
||||
// Scale with no cropping
|
||||
} else {
|
||||
processedImage = [processedImage imageByScalingNotCroppingForSize:options.targetSize];
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare self.metadata, which replicates the logic from processImage: for the UIImagePickerController
|
||||
// self.metadata which will be set to the image in resultForImage:
|
||||
if (metadata.count > 0) {
|
||||
self.metadata = [NSMutableDictionary dictionary];
|
||||
|
||||
@@ -451,19 +471,18 @@ static NSString* MIME_JPEG = @"image/jpeg";
|
||||
}
|
||||
}
|
||||
|
||||
// Mimic the info dictionary which would be created by UIImagePickerController
|
||||
// Add image, which will be used in retrieveImage: to get the image and do processing
|
||||
NSMutableDictionary *info = [@{UIImagePickerControllerOriginalImage : image} mutableCopy];
|
||||
// Return Cordova result to WebView
|
||||
// Needed weakSelf for completion block
|
||||
__weak CDVCamera* weakSelf = self;
|
||||
|
||||
// Create info dictionary similar to UIImagePickerController
|
||||
// Will be used in retrieveImage: to get the image and do processing like here was done
|
||||
NSMutableDictionary *info = [@{ UIImagePickerControllerOriginalImage : processedImage } mutableCopy];
|
||||
|
||||
// Add metadata if available
|
||||
if (metadata.count > 0) {
|
||||
// This is not used anywhere and can be removed
|
||||
info[UIImagePickerControllerMediaMetadata] = metadata;
|
||||
}
|
||||
|
||||
// Return Cordova result to WebView
|
||||
// Needed weakSelf for completion block
|
||||
__weak CDVCamera* weakSelf = self;
|
||||
|
||||
// Process and return result
|
||||
[self resultForImage:options info:info completion:^(CDVPluginResult* pluginResult) {
|
||||
@@ -695,35 +714,30 @@ static NSString* MIME_JPEG = @"image/jpeg";
|
||||
|
||||
- (UIImage*)retrieveImage:(NSDictionary*)info options:(CDVPictureOptions*)options
|
||||
{
|
||||
UIImage* pickedImage = nil;
|
||||
|
||||
// Get the picked image from info dictionary
|
||||
// get the image
|
||||
UIImage* image = nil;
|
||||
if (options.allowsEditing && [info objectForKey:UIImagePickerControllerEditedImage]) {
|
||||
pickedImage = [info objectForKey:UIImagePickerControllerEditedImage];
|
||||
image = [info objectForKey:UIImagePickerControllerEditedImage];
|
||||
} else {
|
||||
pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
|
||||
image = [info objectForKey:UIImagePickerControllerOriginalImage];
|
||||
}
|
||||
|
||||
// Process image according to options
|
||||
UIImage *processedImage = pickedImage;
|
||||
|
||||
if (options.correctOrientation) {
|
||||
processedImage = [processedImage imageCorrectedForCaptureOrientation];
|
||||
image = [image imageCorrectedForCaptureOrientation];
|
||||
}
|
||||
|
||||
// Scale with optional cropping
|
||||
if (options.targetSize.width > 0 && options.targetSize.height > 0) {
|
||||
// Scale and crop to target size
|
||||
if (options.cropToSize) {
|
||||
processedImage = [processedImage imageByScalingAndCroppingForSize:options.targetSize];
|
||||
|
||||
// Scale with no cropping
|
||||
UIImage* scaledImage = nil;
|
||||
|
||||
if ((options.targetSize.width > 0) && (options.targetSize.height > 0)) {
|
||||
// if cropToSize, resize image and crop to target size, otherwise resize to fit target without cropping
|
||||
if (options.cropToSize) {
|
||||
scaledImage = [image imageByScalingAndCroppingForSize:options.targetSize];
|
||||
} else {
|
||||
processedImage = [processedImage imageByScalingNotCroppingForSize:options.targetSize];
|
||||
scaledImage = [image imageByScalingNotCroppingForSize:options.targetSize];
|
||||
}
|
||||
}
|
||||
|
||||
return processedImage;
|
||||
return (scaledImage == nil ? image : scaledImage);
|
||||
}
|
||||
|
||||
- (void)resultForImage:(CDVPictureOptions*)options
|
||||
@@ -924,7 +938,7 @@ static NSString* MIME_JPEG = @"image/jpeg";
|
||||
|
||||
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
|
||||
{
|
||||
__weak CDVUIImagePickerController* cameraPicker = (CDVUIImagePickerController*)picker;
|
||||
__weak CDVUIImagePickerController* cdvUIImagePickerController = (CDVUIImagePickerController*)picker;
|
||||
__weak CDVCamera* weakSelf = self;
|
||||
|
||||
dispatch_block_t invoke = ^(void) {
|
||||
@@ -934,9 +948,9 @@ static NSString* MIME_JPEG = @"image/jpeg";
|
||||
|
||||
// Image selected
|
||||
if ([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
|
||||
[weakSelf resultForImage:cameraPicker.pictureOptions info:info completion:^(CDVPluginResult* res) {
|
||||
[weakSelf resultForImage:cdvUIImagePickerController.pictureOptions info:info completion:^(CDVPluginResult* res) {
|
||||
if (![self usesGeolocation] || picker.sourceType != UIImagePickerControllerSourceTypeCamera) {
|
||||
[weakSelf.commandDelegate sendPluginResult:res callbackId:cameraPicker.callbackId];
|
||||
[weakSelf.commandDelegate sendPluginResult:res callbackId:cdvUIImagePickerController.callbackId];
|
||||
weakSelf.hasPendingOperation = NO;
|
||||
weakSelf.cdvUIImagePickerController = nil;
|
||||
}
|
||||
@@ -945,13 +959,13 @@ static NSString* MIME_JPEG = @"image/jpeg";
|
||||
// Video selected
|
||||
} else {
|
||||
result = [weakSelf resultForVideo:info];
|
||||
[weakSelf.commandDelegate sendPluginResult:result callbackId:cameraPicker.callbackId];
|
||||
[weakSelf.commandDelegate sendPluginResult:result callbackId:cdvUIImagePickerController.callbackId];
|
||||
weakSelf.hasPendingOperation = NO;
|
||||
weakSelf.cdvUIImagePickerController = nil;
|
||||
}
|
||||
};
|
||||
|
||||
[[cameraPicker presentingViewController] dismissViewControllerAnimated:YES completion:invoke];
|
||||
[[cdvUIImagePickerController presentingViewController] dismissViewControllerAnimated:YES completion:invoke];
|
||||
}
|
||||
|
||||
// older api calls newer didFinishPickingMediaWithInfo
|
||||
@@ -966,7 +980,7 @@ static NSString* MIME_JPEG = @"image/jpeg";
|
||||
|
||||
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
|
||||
{
|
||||
__weak CDVUIImagePickerController* cameraPicker = (CDVUIImagePickerController*)picker;
|
||||
__weak CDVUIImagePickerController* cdvUIImagePickerController = (CDVUIImagePickerController*)picker;
|
||||
__weak CDVCamera* weakSelf = self;
|
||||
|
||||
dispatch_block_t invoke = ^ (void) {
|
||||
@@ -978,13 +992,13 @@ static NSString* MIME_JPEG = @"image/jpeg";
|
||||
}
|
||||
|
||||
|
||||
[weakSelf.commandDelegate sendPluginResult:result callbackId:cameraPicker.callbackId];
|
||||
[weakSelf.commandDelegate sendPluginResult:result callbackId:cdvUIImagePickerController.callbackId];
|
||||
|
||||
weakSelf.hasPendingOperation = NO;
|
||||
weakSelf.cdvUIImagePickerController = nil;
|
||||
};
|
||||
|
||||
[[cameraPicker presentingViewController] dismissViewControllerAnimated:YES completion:invoke];
|
||||
[[cdvUIImagePickerController presentingViewController] dismissViewControllerAnimated:YES completion:invoke];
|
||||
}
|
||||
|
||||
#pragma mark CLLocationManager
|
||||
@@ -1189,24 +1203,24 @@ static NSString* MIME_JPEG = @"image/jpeg";
|
||||
|
||||
+ (instancetype)createFromPictureOptions:(CDVPictureOptions*)pictureOptions
|
||||
{
|
||||
CDVUIImagePickerController* cameraPicker = [[CDVUIImagePickerController alloc] init];
|
||||
cameraPicker.pictureOptions = pictureOptions;
|
||||
cameraPicker.sourceType = pictureOptions.sourceType;
|
||||
cameraPicker.allowsEditing = pictureOptions.allowsEditing;
|
||||
CDVUIImagePickerController* cdvUIImagePickerController = [[CDVUIImagePickerController alloc] init];
|
||||
cdvUIImagePickerController.pictureOptions = pictureOptions;
|
||||
cdvUIImagePickerController.sourceType = pictureOptions.sourceType;
|
||||
cdvUIImagePickerController.allowsEditing = pictureOptions.allowsEditing;
|
||||
|
||||
if (cameraPicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
|
||||
if (cdvUIImagePickerController.sourceType == UIImagePickerControllerSourceTypeCamera) {
|
||||
// We only allow taking pictures (no video) in this API.
|
||||
cameraPicker.mediaTypes = @[(NSString*)kUTTypeImage];
|
||||
cdvUIImagePickerController.mediaTypes = @[(NSString*)kUTTypeImage];
|
||||
// We can only set the camera device if we're actually using the camera.
|
||||
cameraPicker.cameraDevice = pictureOptions.cameraDirection;
|
||||
cdvUIImagePickerController.cameraDevice = pictureOptions.cameraDirection;
|
||||
} else if (pictureOptions.mediaType == MediaTypeAll) {
|
||||
cameraPicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:cameraPicker.sourceType];
|
||||
cdvUIImagePickerController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:cdvUIImagePickerController.sourceType];
|
||||
} else {
|
||||
NSArray* mediaArray = @[(NSString*)(pictureOptions.mediaType == MediaTypeVideo ? kUTTypeMovie : kUTTypeImage)];
|
||||
cameraPicker.mediaTypes = mediaArray;
|
||||
cdvUIImagePickerController.mediaTypes = mediaArray;
|
||||
}
|
||||
|
||||
return cameraPicker;
|
||||
return cdvUIImagePickerController;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user