[转]裁剪图片为正方形

Posted YouNeedCourage

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[转]裁剪图片为正方形相关的知识,希望对你有一定的参考价值。

原文:http://www.cnblogs.com/shisishao/p/6000999.html

 

#pragma mark 获取裁剪区域
- (CGRect)getImageRect:(UIImage *)tempImage {
    CGRect rect;
    if (tempImage.size.width > tempImage.size.height) {
        rect = CGRectMake((tempImage.size.width-tempImage.size.height)/2, 0, tempImage.size.height, tempImage.size.height);
    } else if (tempImage.size.width < tempImage.size.height) {
        rect = CGRectMake(0, (tempImage.size.height-tempImage.size.width)/2, tempImage.size.width, tempImage.size.width);
    } else {
        rect = CGRectMake(0, 0, tempImage.size.width, tempImage.size.width);
    }
    return rect;
}

#pragma mark 裁剪图片
- (UIImage *)getCropImage:(UIImage *)image andRect:(CGRect)rect {
    rect = CGRectMake(ceilf(rect.origin.x), ceilf(rect.origin.y), ceilf(rect.size.width), ceilf(rect.size.height));
    UIGraphicsBeginImageContext(rect.size);
    [image drawAtPoint:CGPointMake(-rect.origin.x, -rect.origin.y)];
    UIImage *cropImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return cropImage;
}

 

以上是关于[转]裁剪图片为正方形的主要内容,如果未能解决你的问题,请参考以下文章

安卓手机怎么斜着裁剪图片,一张正方形图片我想把它斜着切掉一半

JS+JAVA裁图

裁剪和旋转图片 OpenCV

使用 mesh 实现多边形裁剪图片!Cocos Creator!

在 PHP 中将图像裁剪为正方形,而不是调整为相同的纵横比

PHP裁剪图像 - 错误区域