Skip to content

Instantly share code, notes, and snippets.

@agassiyzh
Created November 9, 2011 21:16
Show Gist options
  • Save agassiyzh/1353062 to your computer and use it in GitHub Desktop.
Save agassiyzh/1353062 to your computer and use it in GitHub Desktop.
UIImage切图
@interface UIImage (CutSize)
- (UIImage*)cutToSize:(CGSize)size;
@end
#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