//如果图片大于maxSize,将图片高宽等比例缩放至maxSize
- (UIImage *)imageWithConstrainSize:(CGSize)maxSize
{
if(self.size.width <= maxSize.width && self.size.height <= maxSize.height) return self;
CGFloat scale = MAX(maxSize.width/self.size.width, maxSize.height/self.size.height);
CGSize scaleSize = CGSizeMake(scale * self.size.width, scale * self.size.height);
UIGraphicsBeginImageContext(scaleSize);
// 绘制改变大小的图片
[self drawInRect:CGRectMake(0, 0, scaleSize.width, scaleSize.height)];
// 从当前context中创建一个改变大小后的图片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();
// 返回新的改变大小后的图片
return scaledImage;
}