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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 09-26-2011, 09:17 AM   #26 (permalink)
Registered Member
 
Join Date: Jun 2010
Location: Bostonia
Posts: 53
evan1466 is on a distinguished road
Default Here is my modified version. Has some work arounds for retina and iPad 3.2 vs 4.0.

Code:
-(UIImage*)imageByScalingProportionallyToMinimumSize:(UIImage *)imageIn :(CGSize)targetSize{	
	UIImage *sourceImage = imageIn;
	CGSize imageSize = sourceImage.size;
	CGFloat width = imageSize.width;
	CGFloat height = imageSize.height;
	CGFloat targetWidth = targetSize.width;
	CGFloat targetHeight = targetSize.height;    
    if([[UIScreen mainScreen]respondsToSelector:@selector(scale)]&&(UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad)) { 
		CGFloat scale = [[UIScreen mainScreen] scale];
		targetWidth = targetWidth*scale;
		targetHeight = targetHeight*scale;
	}
	CGFloat scaleFactor = 1.0;
	CGFloat scaledWidth = targetWidth;
	CGFloat scaledHeight = targetHeight;
	
	CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
	
	if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
		
		CGFloat widthFactor = targetWidth / width;
		CGFloat heightFactor = targetHeight / height;
		
		if (widthFactor > heightFactor)
			scaleFactor = widthFactor;
		else
			scaleFactor = heightFactor;
		
		scaledWidth  = width * scaleFactor;
		scaledHeight = height * scaleFactor;
		
		if (widthFactor > heightFactor) {
			thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
		} else if (widthFactor < heightFactor) {
			thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
		}
	}
	CGImageRef imageRef = [sourceImage CGImage];
	CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
	CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB();
	
	if (alphaInfo == kCGImageAlphaNone)
		alphaInfo = kCGImageAlphaNoneSkipLast;
	
	CGContextRef bitmap;
	
	if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
		bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), 4*targetWidth, colorSpaceInfo, alphaInfo);
		
	} else {
		bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), 4*targetWidth, colorSpaceInfo, alphaInfo);
		
	}
	if (sourceImage.imageOrientation == UIImageOrientationLeft) {
		NSLog(@"image orientation left");
		CGContextRotateCTM (bitmap, radians(90));
		CGContextTranslateCTM (bitmap, 0, -height);
		
	} else if (sourceImage.imageOrientation == UIImageOrientationRight) {
		NSLog(@"image orientation right");
		CGContextRotateCTM (bitmap, radians(-90));
		CGContextTranslateCTM (bitmap, -width, 0);
		
	} else if (sourceImage.imageOrientation == UIImageOrientationUp) {
		NSLog(@"image orientation up");	
		
	} else if (sourceImage.imageOrientation == UIImageOrientationDown) {
		NSLog(@"image orientation down");	
		CGContextTranslateCTM (bitmap, targetWidth,targetHeight);
		CGContextRotateCTM (bitmap, radians(-180.));
	}
    
	CGContextDrawImage(bitmap, CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledWidth, scaledHeight), imageRef);
	CGImageRef ref = CGBitmapContextCreateImage(bitmap);
	UIImage *result;
    if([[UIScreen mainScreen]respondsToSelector:@selector(scale)]&&(UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad)) { 
		float scale = [[UIScreen mainScreen] scale];
		result = [UIImage imageWithCGImage:ref scale:scale orientation:UIImageOrientationUp];
	} else {
		result = [UIImage imageWithCGImage:ref];
	}
	CGColorSpaceRelease(colorSpaceInfo);
	CGContextRelease(bitmap);
	CGImageRelease(ref);
	
	return result;	
}
evan1466 is offline   Reply With Quote
Old 09-26-2011, 10:44 AM   #27 (permalink)
Registered Member
 
cmezak's Avatar
 
Join Date: Jul 2008
Posts: 207
cmezak is on a distinguished road
Default

Quote:
Originally Posted by evan1466 View Post
So I am using very similar code to what is above.

Have been having issues resizing PNGs but only in the simulator.

Any thoughts?

CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component colorspace; kCGImageAlphaLast; 970 bytes/row.
It's been a long time since I've participated in this thread. I'm now using code from this fella to handle my image resizing:

Resize a UIImage the right way — Trevor’s Bike Shed

Good luck!
cmezak is offline   Reply With Quote
Old 09-26-2011, 12:33 PM   #28 (permalink)
Registered Member
 
Join Date: Jun 2010
Location: Bostonia
Posts: 53
evan1466 is on a distinguished road
Default

So looking into it his code has the same issue on the simulator.

Something with the alpha setting on the PNGs isn't flowing through and causes the unsupported warning.

Oddly it works on the device, which I guess is all that really matters...



Quote:
Originally Posted by cmezak View Post
It's been a long time since I've participated in this thread. I'm now using code from this fella to handle my image resizing:

Resize a UIImage the right way — Trevor’s Bike Shed

Good luck!
__________________
Match aPhoto - Your Memory Game!
evan1466 is offline   Reply With Quote
Reply

Bookmarks

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: 378
14 members and 364 guests
cpsclicker, dre, Error404, Gaz, gmarro, jeroenkeij, Kirkout, mottdog, Music Man, PavelMik, teebee74, whitey99, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

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