拼接头像组

1,根据图片url下载图片


/**
 根据网络图片,得到群组头像

如果是网络图片,需要先把图片下载好。
 @param URLArray 头像地址
 @param bgColor 图片填充色
 @return 群组头像
 */
+ (UIImage *)groupImageWithURLS:(NSArray<NSString *> *)URLArray backgroundColor:(UIColor *)bgColor imageWidth:(CGFloat)imageWidth{

    if (!URLArray) return nil;
    
    NSMutableArray<UIImage *> *imageArray = [NSMutableArray arrayWithCapacity:URLArray.count];
    for (NSString *urlStr in URLArray) {
        NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:urlStr]];
        UIImage *image = [[UIImage alloc] initWithData:imageData];
        if (imageData != nil)
            [imageArray addObject:image];
    }
    if (imageArray.count == 1) {
        return imageArray.firstObject;
    }
    
    return [UIImage groupImageWithImages:imageArray backgroundColor:bgColor imageSize:imageWidth];
}


2,组装下载好的图片


/**
 根据图片合成群组头像

 @param imageArray 图片数组
 @param bgColor 填充色
 @param imageWidth 图片宽度
 @return 群组头像
 */
+ (UIImage *)groupImageWithImages:(NSArray<UIImage *> *)imageArray backgroundColor:(UIColor *)bgColor imageSize:(CGFloat)imageWidth {

    if (imageArray == nil) return nil;
    if (imageArray.count < 2) return imageArray.firstObject;
    
    UIColor *drawBackgroundColor = bgColor==nil?[UIColor groupTableViewBackgroundColor]:bgColor;
    // 计算各个图片的位置
    NSArray<NSString *> *rectArray = [UIImage imageRectWithCount:imageArray.count drawSize:imageWidth];
    // 布局画布
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageWidth, imageWidth), NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, drawBackgroundColor.CGColor);
    CGContextSetStrokeColorWithColor(context, drawBackgroundColor.CGColor);
    CGContextSetLineWidth(context, 1.f);
    CGContextMoveToPoint(context, 0, 0);
    CGContextAddLineToPoint(context, 0, imageWidth);
    CGContextAddLineToPoint(context, imageWidth, imageWidth);
    CGContextAddLineToPoint(context, imageWidth, 0);
    CGContextAddLineToPoint(context, 0, 0);
    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathFillStroke);
    
    for (int i = 0; i<imageArray.count; i++) {
        UIImage *image = imageArray[i];
        CGRect rect = CGRectFromString(rectArray[i]);
        [image drawInRect:rect];
    }
    UIImage *groupIconImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return groupIconImage;
}

3,具体的绘制


// 根据图片的数量得到每张图片对应的位置
+ (NSArray<NSString *> *)imageRectWithCount:(NSInteger)count drawSize:(CGFloat)size {
    
    NSMutableArray *rectArray = [NSMutableArray arrayWithCapacity:count];
    CGFloat drawWidth = size?size:100.f;
    CGFloat padding = 8.f;          //图片之间的边距
    CGFloat imageWidth = 0.f;       //图片宽度
    
    NSInteger row = 0;          //行
    NSInteger column = 0;       //列
    NSInteger remainder = 0;    //余数
    // 处理图片宽度
    if (count <= 4) {
        imageWidth = (drawWidth - padding*3)/2;
        row = count/2;
        column = 2;
        remainder = count%2;
    }else{
        imageWidth = (drawWidth - padding*4)/3;
        row = count/3;
        column = 3;
        remainder = count%3;
    }
    CGFloat topMargin = 0.f;
    
    if (remainder > 0) {        //有多余行,先布局多出来的一行
        CGFloat rowFirstY = (size - ((row + 1)*imageWidth + row*padding))/2;
        CGFloat rowFirstX = (size - (remainder*imageWidth + (remainder - 1)*padding))/2;
        for (int i = 0; i<remainder; i++) {
            CGFloat x = rowFirstX + (imageWidth+padding)*i;
            CGFloat y = rowFirstY;
            CGRect rect = CGRectMake(x, y, imageWidth, imageWidth);
            NSString *imageRectStr = NSStringFromCGRect(rect);
            [rectArray addObject:imageRectStr];
        }
        topMargin = rowFirstY + imageWidth + padding;
    }else{                      // 没有多余行,顶部边距根据行数正常计算
        topMargin = (size - (imageWidth*row + (row-1)*padding))/2;
    }
    
    //布局其他正常的位置
    for (int i = 0; i<row; i++) {
        for (int j = 0; j<column; j++) {
            CGFloat x = padding + (imageWidth + padding)*j;
            CGFloat y = topMargin + (imageWidth + padding)*i;
            CGRect rect = CGRectMake(x, y, imageWidth, imageWidth);
            NSString *imageRectStr = NSStringFromCGRect(rect);
            [rectArray addObject:imageRectStr];
        }
    }
    
    return rectArray;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值