#pragma mark --缩放图片
+(UIImage *)scaleImage:(UIImage *)image toWidth:(int)toWidth toHeight:(int)toHeight{
int width=0;
int height=0;
int x=0;
int y=0;
if (image.size.width<toWidth)
{
width = toWidth;
height = image.size.height*(toWidth/image.size.width);
y = (height - toHeight)/2;
}
else if (image.size.height<toHeight)
{
height = toHeight;
width = image.size.width*(toHeight/image.size.height);
x = (width - toWidth)/2;
}
else if (image.size.width>toWidth)
{
width = toWidth;
height = image.size.height*(toWidth/image.size.width);
y = (height - toHeight)/2;
}
else if (image.size.height>toHeight)
{
height = toHeight;
width = image.size.width*(toHeight/image.size.height);
x = (width - toWidth)/2;
}
else
{
height = toHeight;
width = toWidth;
}
CGSize size = CGSizeMake(width, height);
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGSize subImageSize = CGSizeMake(toWidth, toHeight);
CGRect subImageRect = CGRectMake(x, y, toWidth, toHeight);
CGImageRef imageRef = image.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, subImageRect);
UIGraphicsBeginImageContext(subImageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, subImageRect, subImageRef);
UIImage* subImage = [UIImage imageWithCGImage:subImageRef];
CGImageRelease(subImageRef);
UIGraphicsEndImageContext();
return subImage;
}
#pragma mark --用颜色生成图片
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
@autoreleasepool {
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context,
color.CGColor);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
}