Created
November 9, 2011 21:16
-
-
Save agassiyzh/1353062 to your computer and use it in GitHub Desktop.
UIImage切图
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface UIImage (CutSize) | |
- (UIImage*)cutToSize:(CGSize)size; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "UIImage+CutSize.h" | |
@implementation UIImage (CutSize) | |
- (UIImage*)cutToSize:(CGSize)size{ | |
UIImage *newImage; | |
if (size.width/size.height - self.size.width/self.size.height > 0.01f) { | |
CGSize imageSize = self.size; | |
CGFloat scale = imageSize.width / size.width; | |
CGRect rect = CGRectMake(0.0f, (imageSize.height - size.height *scale)/2, size.width*scale, size.height *scale); | |
CGImageRef imageRef=CGImageCreateWithImageInRect([self CGImage],rect); | |
newImage = [UIImage imageWithCGImage:imageRef]; | |
return newImage; | |
}else if (size.height/size.width - self.size.height/self.size.width> 0.01f) { | |
CGSize size = self.size; | |
CGFloat scale = size.height / size.height; | |
CGRect rect = CGRectMake((size.width - size.width*scale)/2, 0.0f, size.width*scale, size.height*scale); | |
CGImageRef imageRef=CGImageCreateWithImageInRect([self CGImage],rect); | |
newImage = [UIImage imageWithCGImage:imageRef]; | |
return newImage; | |
} | |
return self; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment