截图
#pragma mark --裁剪
- (UIImage *)handleImage:(UIImage *)originalImage withSize(CGSize)size
{ }
- (UIImage *)handleImage:(UIImage *)originalImage withSize:(CGSize )size {
CGSize originalsize = [originalImage size];
NSLog(@"高度 == %f 宽度 == %f", originalsize.height,originalsize.width);
//原图长|宽均小于标准长宽的,不作处理 直接返回
if (originalsize.width < size.width && originalsize.height < size.height) {
return originalImage;
}
//原图长宽均大于标准长|宽,按比例适配
else if (originalsize.width > size.width && originalsize.height > size.height) {
CGFloat rate = 1.f;
CGFloat widthRate = originalsize.width/originalsize.height;//宽:高
CGFloat heightRate = originalsize.height/originalsize.width;//高:宽
rate = widthRate > heightRate ? heightRate:widthRate;
CGImageRef imageRef = nil;
if (heightRate > widthRate)
{
imageRef = CGImageCreateWithImageInRect([originalImage CGImage], CGRectMake(0, originalsize.height/2 - size.height *rate/2, originalsize.width, size.height*rate));//获取图片整体部分
}
else
{
imageRef = CGImageCreateWithImageInRect([originalImage CGImage], CGRectMake(originalsize.width/2 - size.width*rate/2, 0, size.width*rate, originalsize.height));//获取图片整体部分
}
UIGraphicsBeginImageContext(size);//指定要绘画图片的大小
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(con, 0.0, size.height);
CGContextScaleCTM(con, 1.0, -1.0);
CGContextDrawImage(con, CGRectMake(0, 0, size.width, size.height), imageRef);
UIImage * standardImage = UIGraphicsGetImageFromCurrentImageContext();
NSLog(@"裁剪之后的图片: 宽度 %f 高度 %f",[standardImage size].width,[standardImage size].height);
UIGraphicsEndImageContext();
CGImageRelease(imageRef);
return standardImage;
}
//原图长|宽有一项大于标准长|宽的,对大于标准的那一项进行裁剪,另一项保持不变
else if (originalsize.width > size.width || originalsize.height > size.height) {
CGImageRef imageRef = nil;
//高度大于标准高度
if (originalsize.height > size.height) {
imageRef = CGImageCreateWithImageInRect([originalImage CGImage], CGRectMake(0, originalsize.height/2 - size.height/2, originalsize.width, size.height));//获取图片整体部分
}
else if (originalsize.width > size.width) {
imageRef = CGImageCreateWithImageInRect([originalImage CGImage], CGRectMake(originalsize.width/2 - size.width/2, 0, size.width, originalsize.height));//获取图片整体部分
}
UIGraphicsBeginImageContext(size);//指定要绘画图片的大小
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(con, 0.0, size.height);
CGContextScaleCTM(con, 1.0, -1.0);
CGContextDrawImage(con, CGRectMake(0, 0, size.width, size.height), imageRef);
UIImage *standardImage = UIGraphicsGetImageFromCurrentImageContext();
NSLog(@"裁剪之后的图片⭐️: 宽度 %f 高度 %f",[standardImage size].width,[standardImage size].height);
UIGraphicsEndImageContext();
CGImageRelease(imageRef);
return standardImage;
}
//原图为标准长宽的,不做处理
return originalImage;
}