views:

48

answers:

2

I am using

- (void)imagePickerController:(UIImagePickerController *)picker 
  didFinishPickingImage:(UIImage *)image
      editingInfo:(NSDictionary *)editingInfo

to take the image.
Then use

  CGImageRef imgRef = image.CGImage;

 CGFloat width = CGImageGetWidth(imgRef);
 CGFloat height = CGImageGetHeight(imgRef);

For some reason, the width always 640, and height always 480. no matter whether it is portrait or landscape.

I really confused on that, I thought in portraint, it should be width 320 and height is 480, while in landscape mode, width should be 480 and height should be 320.

What am I missing? How do I get correct width and height of the image. Thanks.

+1  A: 

imagePickerController:didFinishPickingImage:editingInfo: is depreciated as of iOS 3.0.

Try using imagePickerController:didFinishPickingMediaWithInfo: and see if it makes a difference.

greg
The result is the same, the image size still 640 * 480.
BlueDolphin
You do have access to a lot more information from the supported message though. You can grab the `UIImage` element `UIImagePickerControllerEditedImage` from the `info` `NSDictionary` and make use of `UIImage`'s properties like `size.width`, `size.height`, and `imageOrientation`. If you really need to get the CGImage, you can grab that as well.
greg
+1  A: 

You should implement something like this:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    CGImageRef ref = image.CGImage;
    int width = CGImageGetWidth(ref);
    int height = CGImageGetHeight(ref);
    NSLog(@"image size = %d x %d", width, height);

    UIImage *orig = [editingInfo objectForKey:UIImagePickerControllerOriginalImage];
    ref = orig.CGImage;
    width = CGImageGetWidth(ref);
    height = CGImageGetHeight(ref);
    NSLog(@"orig image size = %d x %d", width, height);

    CGRect origRect;
    [[editingInfo objectForKey:UIImagePickerControllerCropRect] getValue:&origRect];

    NSLog(@"Crop rect = %f %f %f %f", origRect.origin.x, origRect.origin.y, origRect.size.width, origRect.size.height);
}

For more discussion pease see: http://discussions.apple.com/thread.jspa?messageID=7841245&tstart=0

Deniz Mert Edincik
This is the output:
BlueDolphin
This is the log output: image size = 640 x 480 orig image size = 0 x 0 Crop rect = 0.000000 0.000000 0.207944 0.000000
BlueDolphin
are you getting the image from camera?
Deniz Mert Edincik
I got the image, I just can't handle the size properly.
BlueDolphin
OK if you are getting from camera/camera roll, I doubt that phone knows anything about orientation.
Deniz Mert Edincik
Seems [info objectForKey:UIImagePickerControllerOriginalImage] size.width and height gives proper way. It is 640 * 480 in landscape and 480 * 640 in portrait.
BlueDolphin