图片合成

两张图片拼接

+ (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/

你可能感兴趣的:(图片合成)