You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by sh...@apache.org on 2012/04/12 01:14:18 UTC

[1/2] ios commit: ios camera targetWidth/Height don't match the documentation Fixes CB-183 and CB-54

Updated Branches:
  refs/heads/master b925f5bcf -> 837ab699a


ios camera targetWidth/Height don't match the documentation Fixes CB-183 and CB-54


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/commit/d5f5c39d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/tree/d5f5c39d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/diff/d5f5c39d

Branch: refs/heads/master
Commit: d5f5c39dcd9b66df1846b1240122c9f11526dcf5
Parents: b925f5b
Author: Ben Birch <bb...@aconex.com>
Authored: Wed Apr 11 17:04:28 2012 +1000
Committer: Shazron Abdullah <sh...@apache.org>
Committed: Wed Apr 11 16:10:23 2012 -0700

----------------------------------------------------------------------
 CordovaLib/Classes/CDVCamera.h       |    8 +++++
 CordovaLib/Classes/CDVCamera.m       |   48 ++++++++++++++++++++++++++++-
 CordovaLib/javascript/cordova.ios.js |   12 ++++++-
 3 files changed, 65 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/d5f5c39d/CordovaLib/Classes/CDVCamera.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVCamera.h b/CordovaLib/Classes/CDVCamera.h
index de58637..4cdd45f 100644
--- a/CordovaLib/Classes/CDVCamera.h
+++ b/CordovaLib/Classes/CDVCamera.h
@@ -39,6 +39,12 @@ enum CDVMediaType {
 };
 typedef NSUInteger CDVMediaType;
 
+enum CDVSizingMethod {
+		SizingMethodContain = 0,
+		SizingMethodCover
+};
+typedef NSUInteger CDVSizingMethod;
+
 @interface CDVCameraPicker : UIImagePickerController
 {
 }
@@ -53,6 +59,7 @@ typedef NSUInteger CDVMediaType;
 @property (assign) CGSize targetSize;
 @property (assign) bool correctOrientation;
 @property (assign) bool saveToPhotoAlbum;
+@property (nonatomic) enum CDVSizingMethod sizingMethod;
 
 - (void) dealloc;
 
@@ -86,6 +93,7 @@ typedef NSUInteger CDVMediaType;
 - (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(NSDictionary*)editingInfo;
 - (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker;
 - (UIImage*)imageByScalingAndCroppingForSize:(UIImage*)anImage toSize:(CGSize)targetSize;
+- (UIImage*)imageByScalingNotCroppingForSize:(UIImage*)anImage toSize:(CGSize)frameSize;
 - (UIImage*)imageCorrectedForCaptureOrientation:(UIImage*)anImage;
 - (void) dealloc;
 

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/d5f5c39d/CordovaLib/Classes/CDVCamera.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVCamera.m b/CordovaLib/Classes/CDVCamera.m
index a079b40..3e3f4e0 100644
--- a/CordovaLib/Classes/CDVCamera.m
+++ b/CordovaLib/Classes/CDVCamera.m
@@ -44,6 +44,7 @@
  *  8       allowsEdit
  *  9       correctOrientation
  *  10      saveToPhotoAlbum
+ *  11	    sizingMethod
  */
 - (void) takePicture:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
 {
@@ -85,6 +86,7 @@
         self.pickerController.allowsEditing = allowEdit; // THIS IS ALL IT TAKES FOR CROPPING - jm
         self.pickerController.callbackId = callbackId;
         self.pickerController.targetSize = targetSize;
+        self.pickerController.sizingMethod = ([arguments objectAtIndex:11]) ? [[arguments objectAtIndex:11] intValue ] : SizingMethodContain;
         self.pickerController.correctOrientation = [[arguments objectAtIndex:9] boolValue];
         self.pickerController.saveToPhotoAlbum = [[arguments objectAtIndex:10] boolValue];
         
@@ -180,7 +182,12 @@
     UIImage *scaledImage = nil;
     
     if (self.pickerController.targetSize.width > 0 && self.pickerController.targetSize.height > 0) {
-        scaledImage = [self imageByScalingAndCroppingForSize:image toSize:self.pickerController.targetSize];
+        // if preventCropping, resize image to fit within targetSize without cropping
+        if(self.pickerController.sizingMethod == SizingMethodContain) {
+            scaledImage = [self imageByScalingNotCroppingForSize:image toSize:self.pickerController.targetSize];
+        } else if(self.pickerController.sizingMethod == SizingMethodCover) {
+            scaledImage = [self imageByScalingAndCroppingForSize:image toSize:self.pickerController.targetSize];
+        }
     }
     NSData* data = nil;
 		if (self.pickerController.encodingType == EncodingTypePNG) {
@@ -366,6 +373,44 @@
    return newImage;
 }
 
+- (UIImage*)imageByScalingNotCroppingForSize:(UIImage*)anImage toSize:(CGSize)frameSize
+{
+    UIImage *sourceImage = anImage;
+    UIImage *newImage = nil;        
+    CGSize	imageSize = sourceImage.size;
+    CGFloat width = imageSize.width;
+    CGFloat height = imageSize.height;
+    CGFloat targetWidth = frameSize.width;
+    CGFloat targetHeight = frameSize.height;
+    CGFloat scaleFactor = 0.0;
+    CGSize	scaledSize = frameSize;
+    
+    if (CGSizeEqualToSize(imageSize, frameSize) == NO) 
+    {
+        CGFloat widthFactor = targetWidth / width;
+        CGFloat heightFactor = targetHeight / height;
+        
+        // opposite comparison to imageByScalingAndCroppingForSize in order to contain the image within the given bounds
+        if (widthFactor > heightFactor) 
+            scaleFactor = heightFactor; // scale to fit height
+        else
+            scaleFactor = widthFactor; // scale to fit width
+        scaledSize = CGSizeMake(width * scaleFactor, height * scaleFactor);
+    }
+    
+    UIGraphicsBeginImageContext(scaledSize); // this will resize
+    
+    [sourceImage drawInRect:CGRectMake(0, 0, scaledSize.width, scaledSize.height)];
+    
+    newImage = UIGraphicsGetImageFromCurrentImageContext();
+    if(newImage == nil) 
+        NSLog(@"could not scale image");
+    
+    //pop the context to get back to the default
+    UIGraphicsEndImageContext();
+    return newImage;
+}
+
 - (void) postImage:(UIImage*)anImage withFilename:(NSString*)filename toUrl:(NSURL*)url 
 {
 	NSString *boundary = @"----BOUNDARY_IS_I";
@@ -427,6 +472,7 @@
 @synthesize correctOrientation;
 @synthesize saveToPhotoAlbum;
 @synthesize encodingType;
+@synthesize sizingMethod;
 
 
 - (void) dealloc

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/d5f5c39d/CordovaLib/javascript/cordova.ios.js
----------------------------------------------------------------------
diff --git a/CordovaLib/javascript/cordova.ios.js b/CordovaLib/javascript/cordova.ios.js
index 9f7cd45..5e54009 100644
--- a/CordovaLib/javascript/cordova.ios.js
+++ b/CordovaLib/javascript/cordova.ios.js
@@ -1103,13 +1103,17 @@ cameraExport.getPicture = function(successCallback, errorCallback, options) {
     	correctOrientation = options.correctOrientation <=0 ? false : true;
     }
     var saveToPhotoAlbum = false;
-	if (typeof options.saveToPhotoAlbum == "boolean") {
+    if (typeof options.saveToPhotoAlbum == "boolean") {
     	saveToPhotoAlbum = options.saveToPhotoAlbum;
     } else if (typeof options.saveToPhotoAlbum == "number") {
     	saveToPhotoAlbum = options.saveToPhotoAlbum <=0 ? false : true;
     }
+    var sizingMethod = Camera.ResizingMethod.CONTAIN;
+    if (typeof options.sizingMethod == "number") {
+        sizingMethod = options.sizingMethod;
+    }
 
-    exec(successCallback, errorCallback, "Camera", "takePicture", [quality, destinationType, sourceType, targetWidth, targetHeight, encodingType, mediaType, allowEdit, correctOrientation, saveToPhotoAlbum]);
+    exec(successCallback, errorCallback, "Camera", "takePicture", [quality, destinationType, sourceType, targetWidth, targetHeight, encodingType, mediaType, allowEdit, correctOrientation, saveToPhotoAlbum, sizingMethod]);
 }
 
 module.exports = cameraExport;
@@ -1136,6 +1140,10 @@ module.exports = {
     PHOTOLIBRARY : 0,    // Choose image from picture library (same as SAVEDPHOTOALBUM for Android)
     CAMERA : 1,          // Take picture from camera
     SAVEDPHOTOALBUM : 2  // Choose image from picture library (same as PHOTOLIBRARY for Android)
+  },
+  ResizingMethod:{       // the ResizingMethod uses the same resizing algorithm as css3 background-size rule
+    CONTAIN : 0,         // Scale the image, while preserving its intrinsic aspect ratio, to the largest size such that both its width and its height can fit inside the targetHeight/Width
+    COVER : 1            // Scale the image, while preserving its intrinsic aspect ratio, to the smallest size such that both its width and its height can completely cover the targetHeight/Width
   }
 };