按大小裁剪为指定的尺寸并解决图片裁剪之后显示不正确的问题
+ (UIImage *)cutCenterImageSize:(CGSize)size iMg:(UIImage *)img {
CGFloat scale = [UIScreen mainScreen].scale;
size.width = size.width*scale;
size.height = size.height *scale;
CGSize imageSize = img.size;
CGRect rect;
//根据图片的大小计算出图片中间矩形区域的位置与大小
if (imageSize.width > imageSize.height) {
float leftMargin = (imageSize.width - imageSize.height) *0.5;
rect = CGRectMake(leftMargin,0, imageSize.height, imageSize.height);
}else{
float topMargin = (imageSize.height - imageSize.width) *0.5;
rect = CGRectMake(0, topMargin, imageSize.width, imageSize.width);
}
//记录旋转方向
CGAffineTransform transform = CGAffineTransformIdentity;
switch (img.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, img.size.width, img.size.height);
transform = CGAffineTransformRotate(transform,M_PI);
break;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, img.size.width,0);
transform = CGAffineTransformRotate(transform,M_PI_2);
break;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform,0, img.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
default:
break;
}
CGImageRef imageRef = img.CGImage;
//截取中间区域矩形图片
CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
UIImage *tmp = [[UIImage alloc] initWithCGImage:imageRefRect];
CGImageRelease(imageRefRect);
UIGraphicsBeginImageContext(size);
CGRect rectDraw =CGRectMake(0,0, size.width, size.height);
[tmp drawInRect:rectDraw];
// 从当前context中创建一个改变大小后的图片
tmp = UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();
// 旋转为正确的方向
CGContextRef ctx = CGBitmapContextCreate(NULL, tmp.size.width, tmp.size.height,
CGImageGetBitsPerComponent(tmp.CGImage),0,
CGImageGetColorSpace(tmp.CGImage),
CGImageGetBitmapInfo(tmp.CGImage));
CGContextConcatCTM(ctx, transform);
return tmp;
}
网友评论