123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- //
- // UIImage-Extensions.m
- // CutPicetureDemo
- //
- // Created by yang kong on 12-6-27.
- // Copyright 2012 __MyCompanyName__. All rights reserved.
- //
- #import "UIImage-Extensions.h"
- CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
- CGFloat RadiansToDegrees(CGFloat radians) {return radians * 180/M_PI;};
- @implementation UIImage (CS_Extensions)
- -(UIImage *)imageAtRect:(CGRect)rect
- {
-
- CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
- UIImage* subImage = [UIImage imageWithCGImage: imageRef];
- CGImageRelease(imageRef);
-
- return subImage;
-
- }
- - (UIImage *)imageByScalingProportionallyToMinimumSize:(CGSize)targetSize {
-
- UIImage *sourceImage = self;
- UIImage *newImage = nil;
-
- CGSize imageSize = sourceImage.size;
- CGFloat width = imageSize.width;
- CGFloat height = imageSize.height;
-
- CGFloat targetWidth = targetSize.width;
- CGFloat targetHeight = targetSize.height;
-
- CGFloat scaleFactor = 0.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;
-
- // center the image
-
- if (widthFactor > heightFactor) {
- thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
- } else if (widthFactor < heightFactor) {
- thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
- }
- }
-
-
- // this is actually the interesting part:
-
- UIGraphicsBeginImageContext(targetSize);
-
- CGRect thumbnailRect = CGRectZero;
- thumbnailRect.origin = thumbnailPoint;
- thumbnailRect.size.width = scaledWidth;
- thumbnailRect.size.height = scaledHeight;
-
- [sourceImage drawInRect:thumbnailRect];
-
- newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
- if(newImage == nil) NSLog(@"could not scale image");
-
-
- return newImage ;
- }
- - (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize {
-
- UIImage *sourceImage = self;
- UIImage *newImage = nil;
-
- CGSize imageSize = sourceImage.size;
- CGFloat width = imageSize.width;
- CGFloat height = imageSize.height;
-
- CGFloat targetWidth = targetSize.width;
- CGFloat targetHeight = targetSize.height;
-
- CGFloat scaleFactor = 0.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;
-
- // center the image
-
- if (widthFactor < heightFactor) {
- thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
- } else if (widthFactor > heightFactor) {
- thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
- }
- }
-
-
- // this is actually the interesting part:
-
- UIGraphicsBeginImageContext(targetSize);
-
- CGRect thumbnailRect = CGRectZero;
- thumbnailRect.origin = thumbnailPoint;
- thumbnailRect.size.width = scaledWidth;
- thumbnailRect.size.height = scaledHeight;
-
- [sourceImage drawInRect:thumbnailRect];
-
- newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
- if(newImage == nil) NSLog(@"could not scale image");
-
-
- return newImage ;
- }
- - (UIImage *)imageByScalingToSize:(CGSize)targetSize {
-
- UIImage *sourceImage = self;
- UIImage *newImage = nil;
-
- // CGSize imageSize = sourceImage.size;
- // CGFloat width = imageSize.width;
- // CGFloat height = imageSize.height;
-
- CGFloat targetWidth = targetSize.width;
- CGFloat targetHeight = targetSize.height;
-
- // CGFloat scaleFactor = 0.0;
- CGFloat scaledWidth = targetWidth;
- CGFloat scaledHeight = targetHeight;
-
- CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
-
- // this is actually the interesting part:
-
- UIGraphicsBeginImageContext(targetSize);
-
- CGRect thumbnailRect = CGRectZero;
- thumbnailRect.origin = thumbnailPoint;
- thumbnailRect.size.width = scaledWidth;
- thumbnailRect.size.height = scaledHeight;
-
- [sourceImage drawInRect:thumbnailRect];
-
- newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
- if(newImage == nil) NSLog(@"could not scale image");
-
-
- return newImage ;
- }
- - (UIImage *)imageByScalingToScale:(CGFloat)toScale{
- UIImage *sourceImage = self;
- CGSize targetSize = CGSizeMake(self.size.width*toScale, self.size.height*toScale);
-
- UIGraphicsBeginImageContext(targetSize);
- [sourceImage drawInRect:CGRectMake(0, 0, targetSize.width, targetSize.height)];
- UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
- if (scaledImage == nil) {
- NSLog(@"could not scale image");
- }
-
- return scaledImage;
- }
- - (UIImage *)cutImageBy:(CGSize)oSize{
- CGSize newSize;
- CGImageRef imageRef = nil;
-
- if ((self.size.width / self.size.height) < (oSize.width / oSize.height)) {
- newSize.width = self.size.width;
- newSize.height = self.size.width * oSize.height / oSize.width;
-
- imageRef = CGImageCreateWithImageInRect([self CGImage], CGRectMake(0, fabs(self.size.height - newSize.height) / 2, newSize.width, newSize.height));
- } else {
- newSize.height = self.size.height;
- newSize.width = self.size.height * oSize.width / oSize.height;
-
- imageRef = CGImageCreateWithImageInRect([self CGImage], CGRectMake(fabs(self.size.width - newSize.width) / 2, 0, newSize.width, newSize.height));
- }
-
- return [UIImage imageWithCGImage:imageRef];
- }
- - (UIImage *)imageRotatedByRadians:(CGFloat)radians
- {
- return [self imageRotatedByDegrees:RadiansToDegrees(radians)];
- }
- - (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
- {
- // calculate the size of the rotated view's containing box for our drawing space
- UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];
- CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
- rotatedViewBox.transform = t;
- CGSize rotatedSize = rotatedViewBox.frame.size;
- // [rotatedViewBox release];
-
- // Create the bitmap context
- UIGraphicsBeginImageContext(rotatedSize);
- CGContextRef bitmap = UIGraphicsGetCurrentContext();
-
- // Move the origin to the middle of the image so we will rotate and scale around the center.
- CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
-
- // // Rotate the image context
- CGContextRotateCTM(bitmap, DegreesToRadians(degrees));
-
- // Now, draw the rotated/scaled image into the context
- CGContextScaleCTM(bitmap, 1.0, -1.0);
- CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);
-
- UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return newImage;
-
- }
- @end;
|