Revision: 27102
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at May 22, 2010 10:21 by aviddv1
Initial Code
- (UIImage*)straightenAndScaleImage:(UIImage *)theFullImage andTheMaxDimension:(int)maxDimension { CGImageRef img = [theFullImage CGImage]; CGFloat width = CGImageGetWidth(img); CGFloat height = CGImageGetHeight(img); CGRect bounds = CGRectMake(0, 0, width, height); CGSize size = bounds.size; if (width > maxDimension || height > maxDimension) { CGFloat ratio = width/height; if (ratio > 1.0f) { size.width = maxDimension; size.height = size.width / ratio; } else { size.height = maxDimension; size.width = size.height * ratio; } } CGFloat scale = size.width/width; CGAffineTransform transform = orientationTransformForImage(theFullImage, &size); //CGAffineTransform transform = orientationTransformForImage(theFullImage, &size); UIGraphicsBeginImageContext(size); CGContextRef context = UIGraphicsGetCurrentContext(); // Flip UIImageOrientation orientation = [theFullImage imageOrientation]; if (orientation == UIImageOrientationRight || orientation == UIImageOrientationLeft) { CGContextScaleCTM(context, -scale, scale); CGContextTranslateCTM(context, -height, 0); }else { CGContextScaleCTM(context, scale, -scale); CGContextTranslateCTM(context, 0, -height); } CGContextConcatCTM(context, transform); CGContextDrawImage(context, bounds, img); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } CGAffineTransform orientationTransformForImage(UIImage *image, CGSize *newSize) { CGImageRef img = [image CGImage]; CGFloat width = CGImageGetWidth(img); CGFloat height = CGImageGetHeight(img); CGSize size = CGSizeMake(width, height); CGAffineTransform transform = CGAffineTransformIdentity; CGFloat origHeight = size.height; UIImageOrientation orient = image.imageOrientation; switch(orient) { /* EXIF 1 to 8 */ case UIImageOrientationUp: break; case UIImageOrientationUpMirrored: transform = CGAffineTransformMakeTranslation(width, 0.0f); transform = CGAffineTransformScale(transform, -1.0f, 1.0f); break; case UIImageOrientationDown: transform = CGAffineTransformMakeTranslation(width, height); transform = CGAffineTransformRotate(transform, M_PI); break; case UIImageOrientationDownMirrored: transform = CGAffineTransformMakeTranslation(0.0f, height); transform = CGAffineTransformScale(transform, 1.0f, -1.0f); break; case UIImageOrientationLeftMirrored: size.height = size.width; size.width = origHeight; transform = CGAffineTransformMakeTranslation(height, width); transform = CGAffineTransformScale(transform, -1.0f, 1.0f); transform = CGAffineTransformRotate(transform, 3.0f * M_PI / 2.0f); break; case UIImageOrientationLeft: size.height = size.width; size.width = origHeight; transform = CGAffineTransformMakeTranslation(0.0f, width); transform = CGAffineTransformRotate(transform, 3.0f * M_PI / 2.0f); break; case UIImageOrientationRightMirrored: size.height = size.width; size.width = origHeight; transform = CGAffineTransformMakeScale(-1.0f, 1.0f); transform = CGAffineTransformRotate(transform, M_PI / 2.0f); break; case UIImageOrientationRight: size.height = size.width; size.width = origHeight; transform = CGAffineTransformMakeTranslation(height, 0.0f); transform = CGAffineTransformRotate(transform, M_PI / 2.0f); break; default: ; } *newSize = size; return transform; }
Initial URL
Initial Description
Usage: [self straightenAndScaleImage:imageView.image andTheMaxDimension:100];
Initial Title
Straighten and Scale UIImage
Initial Tags
Initial Language
Objective C