码迷,mamicode.com
首页 > 其他好文 > 详细

[转]裁剪图片为正方形

时间:2017-09-27 13:25:59      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:htm   size   and   cgpoint   get   width   div   turn   graph   

原文: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;
}

 

[转]裁剪图片为正方形

标签:htm   size   and   cgpoint   get   width   div   turn   graph   

原文地址:http://www.cnblogs.com/siasyl/p/7600851.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!