Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-21-2009, 10:43 AM   #1 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 622
Question Resizing Photo, Instead of Cropping

I am taking an image from the camera and want to resize it to new dimensions. When I say resize, I mean I want to keep all of the photo and just make it smaller so that it will fit into a context that I can manipulate (I understand the limit of a cgcontext is 1024). What I have is making a new image with the dimensions I want, but it is doing by cropping the photo instead of resizing it. So I am losing some of the image. What I need is to scale it down.

Any help is appreciated. I've been through several other threads and must be close, but have gotten stuck. Thanks.

Code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
	// create a new bitmap image context to fit in maximum image dimension for cgcontext of 1024.
	UIGraphicsBeginImageContext(CGSizeMake(768, 1024));		
	CGContextRef context = UIGraphicsGetCurrentContext();		
        // draw the image from the camera into the current context
	[image drawInRect:CGRectMake(0, 0, 768, 1024)];
	// push context to make it current (need to do this manually because we are not drawing in a UIView)
	UIGraphicsPushContext(context);								
	// pop context Removes the current graphics context from the top of the stack, restoring the previous context, balance calls to the UIGraphicsPushContext function
	UIGraphicsPopContext();								
	// get a UIImage from the image context
	UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
	// clean up drawing environment
	UIGraphicsEndImageContext();
	// write the combined image to the photo album
	UIImageWriteToSavedPhotosAlbum(outputImage, nil, nil, nil);
	[[picker parentViewController] dismissModalViewControllerAnimated:YES];	
}

Last edited by DenVog; 05-21-2009 at 10:45 AM.
DenVog is offline   Reply With Quote
Old 05-21-2009, 12:32 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
Join Date: Oct 2008
Location: Toronto, Canada
Posts: 18
Send a message via Skype™ to cbiggin
Default

You have to create your new context at the dimensions of the photo, then draw into the new dimensions. Also, I don't think you need to push/pop the current context. Try this:

Code:
    CGFloat imageWidth = CGImageGetWidth([inImage CGImage]);
    CGFloat imageHeight = CGImageGetHeight([inImage CGImage]);
    UIGraphicsBeginImageContext(CGSizeMake(imageWidth, imageHeight));
    CGRect drawRect = CGRectMake(0, 0, 768, 1024);
    [inImage drawInRect:drawRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
__________________
My iPhone Apps:
KillerSudoku - very difficult variation of Sudoku... popular in UK.
Chris Bosh - his personal app!
PocketCocktails
NerdAlert2000
SixPack App
Easy Recipes
cbiggin is offline   Reply With Quote
Reply

Bookmarks

Tags
camera, context, image editing, resize

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 294
20 members and 274 guests
ADY, AragornSG, BrianSlick, Dani77, Dattee, dre, glenn_sayers, HDshot, HemiMG, JasonR, karlam963, nobre84, prchn4christ, Rudy, spiderguy84, themathminister, tomtom100, viniciusdamone, vogueestylee, vvenkatachallam
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,884
Threads: 89,229
Posts: 380,763
Top Poster: BrianSlick (7,129)
Welcome to our newest member, karlam963
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:18 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0