美文网首页
图片裁剪,合并,创建

图片裁剪,合并,创建

作者: Leon1024 | 来源:发表于2019-03-09 14:53 被阅读0次

#import "LENImageCut.h"

@implementation LENImageCut

+ (UIImage *)imageCutFromCenterWithOriginImage:(UIImage *)image ratio:(CGFloat)ratio {
    // 原图大小,一定要乘以scale,否则如果碰到倍图将不准确
    CGFloat originWidth = image.size.width * image.scale;
    CGFloat originHeight = image.size.height * image.scale;
    // 计算裁剪矩形
    CGFloat height = originWidth / ratio;
    CGFloat width = originWidth;
    if (height > originHeight) {
        height = originHeight;
        width = originHeight * ratio;
    }
    CGFloat x = (originWidth - width) / 2;
    CGFloat y = (originHeight - height) / 2;
    CGRect cutRect = CGRectMake(x, y, width, height);
    
    // 转换UIimage
    CGImageRef cgImage = [image CGImage];
    // 裁剪CGImage
    CGImageRef newCGImage = CGImageCreateWithImageInRect(cgImage, cutRect);
    // 转换成UIImage
    UIImage *resultImage = [UIImage imageWithCGImage:newCGImage];
    cgImage = nil;
    return resultImage;
}

+ (UIImage *)imageIncludeOriginImage:(UIImage *)image ratio:(CGFloat)ratio {
    // 原图大小,一定要乘以scale,否则如果碰到倍图将不准确
    CGFloat originWidth = image.size.width * image.scale;
    CGFloat originHeight = image.size.height * image.scale;
    // 计算背景矩形
    CGFloat width = originHeight * ratio;
    CGFloat height = originHeight;
    if (width < originWidth) {
        width = originWidth;
        height = width / ratio;
    }
    CGSize backgroundSize = CGSizeMake(width, height);
    // 创建背景图片
    UIImage *resultImage = [self imageWithBackgroundSize:backgroundSize addForegroundImage:image];
    return resultImage;
}

// 绘制图片到制定矩形中
+ (UIImage *)imageWithBackgroundSize:(CGSize)backgroundSize addForegroundImage:(UIImage *)foregroundImage {
    // 获取图片上下文(画布)
    UIGraphicsBeginImageContext(backgroundSize);
    // 给画布上色,这里上黑色,下面三步不能少
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, UIColor.blackColor.CGColor);
    CGContextFillRect(context, CGRectMake(0, 0, backgroundSize.width, backgroundSize.height));
    // 创建背景图绘制到画布
    [[UIImage new] drawInRect:CGRectMake(0, 0, backgroundSize.width, backgroundSize.height)];
    // 将前景图居中绘制到画布
    CGFloat width = foregroundImage.size.width * foregroundImage.scale;
    CGFloat height = foregroundImage.size.height * foregroundImage.scale;
    CGFloat x = (backgroundSize.width - width) / 2;
    CGFloat y = (backgroundSize.height - height) / 2;
    [foregroundImage drawAtPoint:CGPointMake(x, y)];
    // 得到合成图片
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    // 释放画布
    UIGraphicsEndImageContext();
    return resultingImage;
}

// 居中合并两张图片
+ (UIImage *)imageWithBackgroundImage:(UIImage *)backgroundImage addForegroundImage:(UIImage *)foregroundImage {
    CGFloat contextWidth = backgroundImage.size.width * backgroundImage.scale;
    CGFloat contextHeight = backgroundImage.size.height * backgroundImage.scale;
    CGSize contextSize = CGSizeMake(contextWidth, contextHeight);
    // 获取图片上下文(画布)
    UIGraphicsBeginImageContext(contextSize);
    // 将背景图绘制到画布
    [backgroundImage drawInRect:CGRectMake(0, 0, contextWidth, contextHeight)];
    // 将前景图居中绘制到画布
    CGFloat width = foregroundImage.size.width * foregroundImage.scale;
    CGFloat height = foregroundImage.size.height * foregroundImage.scale;
    CGFloat x = (contextWidth - width) / 2;
    CGFloat y = (contextHeight - height) / 2;
    [foregroundImage drawAtPoint:CGPointMake(x, y)];
    // 得到合成图片
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    // 释放画布
    UIGraphicsEndImageContext();
    return resultingImage;
}

@end

相关文章

网友评论

      本文标题:图片裁剪,合并,创建

      本文链接:https://www.haomeiwen.com/subject/cgispqtx.html