Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Camera pictures in landscape and portrait (differences)

ioannisioannis Posts: 10Registered Users
edited July 2009 in iPhone SDK Development
I use UIImagePickerController to change a UIImage View from my Camera Roll images. The images are 1600 X 1200 so i do this to resize the image and be as my screen size:
CGFloat imageWidth = CGImageGetWidth([image CGImage]);
CGFloat imageHeight = CGImageGetHeight([image CGImage]);
UIGraphicsBeginImageContext(CGSizeMake(imageWidth, imageHeight));
CGRect drawRect = CGRectMake( (imageWidth/2 -160), (imageHeight/2 - 240), 320, 480);
[image drawInRect:drawRect];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

The problem is with my pictures which are taken in landscape mode. What's the difference between pictures taken in landscape and portrait mode?
I tried:
if (imageWidth == 1600){.....above code}

if (imageWidth == 1200){.....imageView.transform = CGAffineTransformMakeRotation(M_PI / 2);....above code with different width and height}
but it always think that imageWidth == 1600.

Thanks in advance.
Post edited by ioannis on

Replies

  • NewiPhoneDeveloperNewiPhoneDeveloper Posts: 459Registered Users
    edited July 2009
    ioannis wrote: »
    I use UIImagePickerController to change a UIImage View from my Camera Roll images. The images are 1600 X 1200 so i do this to resize the image and be as my screen size:
    CGFloat imageWidth = CGImageGetWidth([image CGImage]);
    CGFloat imageHeight = CGImageGetHeight([image CGImage]);
    UIGraphicsBeginImageContext(CGSizeMake(imageWidth, imageHeight));
    CGRect drawRect = CGRectMake( (imageWidth/2 -160), (imageHeight/2 - 240), 320, 480);
    [image drawInRect:drawRect];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    The problem is with my pictures which are taken in landscape mode. What's the difference between pictures taken in landscape and portrait mode?
    I tried:
    if (imageWidth == 1600){.....above code}
    
    if (imageWidth == 1200){.....imageView.transform = CGAffineTransformMakeRotation(M_PI / 2);....above code with different width and height}
    
    but it always think that imageWidth == 1600.

    Thanks in advance.

    Pictures taken in landscape mode are 1600 x 1200, pictures taken in portrait mode 1200 x 1600. This is of course different for the new iPhone 3GS, as the camera supports a higher resolution. However, pictures taken in landscape mode will always be wider, than those taken in portrait mode. Makes sense?

    Make sure, that your UIImagePicker configuration contains the following line:
    picker.allowsImageEditing = NO;
    

    If you don't set it to NO, you will get weird resolutions, like 320x320.

    Then:
    - (void)imagePickerController:(UIImagePickerController *)currentPicker
    		didFinishPickingImage:(UIImage*)theImage
    				  editingInfo:(NSDictionary*)editingInfo {
    
        if (theImage.size.width > image.size.height) { // image is landscape
    		NSLog(@"width:%f, height:%f", theImage.size.width, theImage.size.height);
    		
    		theImage = [self resizeImage:theImage];
    	}
    	else { //image is portrait
    		//NSLog(@"this image is portrait");
    		theImage = [self rotateImage:theImage];
    	}
    
        //now do with your image whatever you like :)
    }
    
    - (UIImage*)rotateImage:(UIImage*)imageToRotate {
    	CGImageRef imageRef = [imageToRotate CGImage];
    	CGContextRef bitmap;
    	CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
    	CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB();
    	
    	if (alphaInfo == kCGImageAlphaNone) {
    		alphaInfo = kCGImageAlphaNoneSkipLast;
    	}
    	
    	int width, height;
    	
    	
    	width = 480;
    	height = 360; //I noticed, that the result was better, if I resized it to 360 instead of 320 and then manually cropped it. There will be of course a tiny part missing, but if you think about it, it will make sense. If you resize 1600x1200 to 480x320 you can not constrain proportions. Try it in photoshop :)
    	
    	bitmap = CGBitmapContextCreate(NULL, height, width, 8, CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);
    	
    	CGContextRotateCTM (bitmap, radians(-90));
    	CGContextTranslateCTM (bitmap, -width, 0);
    	CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
    	CGImageRef ref = CGBitmapContextCreateImage(bitmap); //first create the image
    	
    	
    	//NSLog(@"cropping image");
            CGImageRef final = CGImageCreateWithImageInRect(ref, CGRectMake(0, 0, 320, 480)); //Now crop it. Never create and crop in one step. In case you do, your code will leak (at least that's what happened to me) and your app will crash after repeating this method 5-10 times.
    	
    	UIImage *result = [UIImage imageWithCGImage:ref];
    
           //don't forget to release, otherwise your code will leak!
    	CGContextRelease(bitmap);
    	CGImageRelease(ref);
    	CGImageRelease(final);
    
    	
    	return result;
    }
    
    - (UIImage*)resizeImage:(UIImage*)imageToResize {
    	CGImageRef imageRef = [imageToResize CGImage];
    	CGContextRef bitmap;
    	CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
    	CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB();
    	
    	if (alphaInfo == kCGImageAlphaNone) {
    		alphaInfo = kCGImageAlphaNoneSkipLast;
    	}
    	
    	int width, height;
    	
    		width = 360;
    		height = 480;
    		bitmap = CGBitmapContextCreate(NULL, width, height, 8, CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);
    	
    	CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
    	CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    	
            //NSLog(@"cropping image");
            CGImageRef final = CGImageCreateWithImageInRect(ref, CGRectMake(0, 0, 320, 480)); //Now crop it. Never create and crop in one step. In case you do, your code will leak (at least that's what happened to me) and your app will crash after repeating this method 5-10 times.
    	
    	UIImage *result = [UIImage imageWithCGImage:ref];
    
           //don't forget to release, otherwise your code will leak!
    	CGContextRelease(bitmap);
    	CGImageRelease(ref);
    	CGImageRelease(final);
    
    	
    	return result;
    
    }
    
    

    Hope, I didn't forget something. Let me know how it goes...
    Websites:<br />
    <a href="http://www.friendlydeveloper.com" target="_blank">Friendlydeveloper</a> - Coding Blog<br />
    <a href="http://www.codingsessions.com" target="_blank">Codingsessions</a> - Live iOS Training<br />
    <br />
    iPhone Apps: <br />
    <a hr
  • NewiPhoneDeveloperNewiPhoneDeveloper Posts: 459Registered Users
    edited July 2009
    Sorry, my code will rotate your landscape image to portrait mode. I guess, you don't need that. Just play with the resize method, I hope my example gives you the right idea. Let me know, if I confused you.
    Websites:<br />
    <a href="http://www.friendlydeveloper.com" target="_blank">Friendlydeveloper</a> - Coding Blog<br />
    <a href="http://www.codingsessions.com" target="_blank">Codingsessions</a> - Live iOS Training<br />
    <br />
    iPhone Apps: <br />
    <a hr
  • ioannisioannis Posts: 10Registered Users
    edited July 2009
    Thanks you so much. I made some changes for adding library (with unknown dimensions) photos and some changes to resizeImage method. I have the result i wanted. Thanks again.:):)
  • NewiPhoneDeveloperNewiPhoneDeveloper Posts: 459Registered Users
    edited July 2009
    ioannis wrote: »
    Thanks you so much. I made some changes for adding library (with unknown dimensions) photos and some changes to resizeImage method. I have the result i wanted. Thanks again.:):)

    Glad, I could help :)
    Websites:<br />
    <a href="http://www.friendlydeveloper.com" target="_blank">Friendlydeveloper</a> - Coding Blog<br />
    <a href="http://www.codingsessions.com" target="_blank">Codingsessions</a> - Live iOS Training<br />
    <br />
    iPhone Apps: <br />
    <a hr
Sign In or Register to comment.