 |
|
 |
|
 |
09-26-2011, 09:17 AM
|
#26 (permalink)
|
|
Registered Member
Join Date: Jun 2010
Location: Bostonia
Posts: 53
|
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;
}
|
|
|
09-26-2011, 10:44 AM
|
#27 (permalink)
|
|
Registered Member
Join Date: Jul 2008
Posts: 207
|
Quote:
Originally Posted by evan1466
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!
|
|
|
09-26-2011, 12:33 PM
|
#28 (permalink)
|
|
Registered Member
Join Date: Jun 2010
Location: Bostonia
Posts: 53
|
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
|
__________________
Match aPhoto - Your Memory Game!
|
|
|
 |
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» 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 |
|