需求:将一张圆形的图片切出中间最大的正方形(说白了就是),使用勾股定理得出:高h = image.size.height / sqrtf(2)、(x, y) 坐标为 (sqrt(2) * h - h) / 2【相等:x=y】
/*
圆形图居中裁剪 image 为正方形
*/
+ (UIImage *)centerClipImage:(UIImage *)image {
if (!image) return nil;
CGRect rect = CGRectZero;
CGFloat h = image.size.height / sqrtf(2);
// 经过勾股定理推算出,(x, y) 坐标为 (sqrt(2) * h - h) / 2
CGFloat xy = (sqrt(2) * h - h) / 2 ;
rect = CGRectMake(xy, xy, h, h);
CGImageRef cg = CGImageCreateWithImageInRect(image.CGImage, rect);
UIImage *clippedImage = [UIImage imageWithCGImage:cg];
CGImageRelease(cg);
return clippedImage;
}