@interface UIImage (extend)
//按比例改变图片大小
-(UIImage*)changeImageSizeWithOriginalImage:(UIImage*)image percent:(float)percent;
//圆形
-(UIImage*)circleImage:(UIImage*)image;
//截取部分图像
-(UIImage*)getSubImage:(CGRect)rect;
//等比例缩放
-(UIImage*)scaleToSize:(CGSize)size;
-(UIImage *)rotateImage:(UIImage *)aImage with:(UIImageOrientation)theorient;
-(UIImage *)fixOrientation:(UIImage *)aImage;
@end
static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth,
float ovalHeight)
{
float fw, fh;
if (ovalWidth == 0 || ovalHeight == 0) {
CGContextAddRect(context, rect);
return;
}
CGContextSaveGState(context);
CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
CGContextScaleCTM(context, ovalWidth, ovalHeight);
fw = CGRectGetWidth(rect) / ovalWidth;
fh = CGRectGetHeight(rect) / ovalHeight;
CGContextMoveToPoint(context, fw, fh/2); // Start at lower right corner
CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); // Top right corner
CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // Top left corner
CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // Lower left corner
CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // Back to lower right
CGContextClosePath(context);
CGContextRestoreGState(context);
}
@implementation UIImage (extend)
//按比例改变图片大小
-(UIImage*)changeImageSizeWithOriginalImage:(UIImage*)image percent:(float)percent
{
// change the image size
UIImage *changedImage=nil;
float iwidth=image.size.width*percent;
float iheight=image.size.height*percent;
if (image.size.width != iwidth && image.size.height != iheight)
{
CGSize itemSize = CGSizeMake(iwidth, iheight);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[image drawInRect:imageRect];
changedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
else
{
changedImage = image;
}
return changedImage;
}
//圆角
-(UIImage *) createRoundedRectImage:(UIImage*)image size:(CGSize)size roundSize:(CGSize)roundSize
{
// the size of CGContextRef
int w = size.width;
int h = size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGRect rect = CGRectMake(0, 0, w, h);
CGContextBeginPath(context);
addRoundedRectToPath(context, rect, roundSize.width, roundSize.height);
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), image.CGImage);
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
UIImage *image2 = [UIImage imageWithCGImage:imageMasked];
CGImageRelease(imageMasked);
return image2;
}
//圆形
-(UIImage*)circleImage:(UIImage*)image
{
CGFloat inset = 0.1f;
UIGraphicsBeginImageContext(image.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2);
CGContextSetStrokeColorWithColor(context, [UIColor clearColor].CGColor);
CGRect rect = CGRectMake(inset, inset, image.size.width - inset * 2.0f, image.size.height - inset * 2.0f);
CGContextAddEllipseInRect(context, rect);
CGContextClip(context);
[image drawInRect:rect];
CGContextAddEllipseInRect(context, rect);
CGContextStrokePath(context);
UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newimg;
}
//将image按照theSize大小等比缩放并裁剪
-(UIImage*