iOS多图合成一张

之前有一个小的需求,分享一张长图。于是琢磨了一下做了一个类别,分享一下方法:

@implementation UIImage (CompossImages)
+ (UIImage *)composeImages:(NSArray<UIImage *> *)arr{

    //间隔
    int margin = 20;
    //左右间距
    int marginL = 15;
    //合成宽度
    CGFloat width = 2*[UIScreen mainScreen].bounds.size.width - 2*marginL;
    //合成高度
    CGFloat height = 0;
    //计算高度
    for (UIImage *img in arr) {
        CGFloat h = img.size.height / (img.size.width/width) + margin;
        height += h;
    }
    //画板大小
    UIGraphicsBeginImageContext(CGSizeMake([UIScreen mainScreen].bounds.size.width*2, height+margin+10));

    //当前y值
    CGFloat y = 20;

    //画图
    for (int i = 0; i<arr.count;i++) {
        UIImage *img = arr[i];
        CGFloat w = img.size.width;
        CGFloat h = img.size.height;
        [img drawInRect:CGRectMake(marginL, y, width, h / (w/width))];
        y += h / (w/width) +margin ;
    }
    CGFloat x = 20;
    CGFloat imgW = x/52.f*428;
    //水印
    [[UIImage imageNamed:@"share_shuiyin"] drawInRect:CGRectMake([UIScreen mainScreen].bounds.size.width*2-imgW-marginL, height+5, imgW, x)];

    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return resultImage;


}

@end

可能会有点吃资源,如果有更好的方法,望网友分享

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值