美文网首页
图片合成

图片合成

作者: 键盘仔 | 来源:发表于2018-08-31 11:28 被阅读8次

    两张图片拼接

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

    相关文章

      网友评论

          本文标题:图片合成

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