图片合成

两张图片拼接

+ (UIImage *)joinImage:(UIImage*)firstImage andImage:(UIImage *)secondImage {
    CGFloat screenShotWidth = CGImageGetWidth(firstImage.CGImage);
    CGFloat screenShotHeight = CGImageGetHeight(firstImage.CGImage);
    CGFloat coverImgWidth = CGImageGetWidth(secondImage.CGImage);
    CGFloat coverImgHeight = CGImageGetHeight(secondImage.CGImage);
    
    
    CGSize imgSize = CGSizeMake(screenShotWidth, screenShotHeight + coverImgHeight);
    CGRect imageRect = CGRectMake(0, 0, imgSize.width, imgSize.height);
    CGRect bottomRect = CGRectMake((screenShotWidth - coverImgWidth)/2, screenShotHeight - 1, coverImgWidth, coverImgHeight);
    
    UIGraphicsBeginImageContextWithOptions(imgSize, NO, [UIScreen mainScreen].scale);
//    UIGraphicsBeginImageContext(imgSize);
    // 获取位图上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
//    UIGraphicsBeginImageContext(imgSize);
    CGContextAddRect(context, imageRect);
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillRect(context, imageRect);
    
    [firstImage drawInRect:CGRectMake(0, 0, screenShotWidth, screenShotHeight)];
    [secondImage drawInRect:bottomRect];
    
    
    // 获取位图
    UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();
    // 关闭位图上下文
    UIGraphicsEndImageContext();
    
    return saveImage;
}

参考:
https://www.feiyeda.top/
http://csshengyao.cn/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。