iOS 群头像的简单实现

我们在做群聊的时候,都会遇到群头像的设置问题,那么类似微信群头像的方式是如何实现的呢
下面有两种方式实现:
1,使用 SDWebImage 下载,计算每一个群员头像在群头像中的位置,不同数量的群员,群头像有不同的UI
根据计算的 rect,把所有群员的头像(最多9张)绘制在一个 image 上
绘制出来的 image 作为群头像
-(void)greateGroupAvatar:(NSArray )group view:(UIView)view finished:(void (^)(UIImage *groupAvatar))finished
{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSInteger avatarCount = group.count > 9 ? 9 : group.count;
CGFloat width = ViewWidth / 3 * 0.85;
CGFloat space3 = (ViewWidth - width * 3) / 4; // 三张图时的边距(图与图之间的边距)
CGFloat space2 = (ViewWidth - width * 2 + space3) / 2; // 两张图时的边距
CGFloat space1 = (ViewWidth - width) / 2; // 一张图时的边距
CGFloat y = avatarCount > 6 ? space3 : (avatarCount > 3 ? space2 : space1);
CGFloat x = avatarCount % 3 == 0 ? space3 : (avatarCount % 3 == 2 ? space2 : space1);
width = avatarCount > 4 ? width : (avatarCount > 1 ? (ViewWidth - 3 * space3) / 2 : ViewWidth ); // 重新计算width;

    if (avatarCount == 1) {                                          // 1,2,3,4 张图不同
        x = 0;
        y = 0;
    }
    if (avatarCount == 2) {
        x = space3;
    } else if (avatarCount == 3) {
        x = (ViewWidth -width)/2;
        y = space3;
    } else if (avatarCount == 4) {
        x = space3;
        y = space3;
    }
    __block NSInteger count = 0;
    //下载图片完成的计数
    for (NSInteger i = avatarCount - 1; i >= 0; i--) {
        NSString *avatarUrl = [group objectAtIndex:i];
        dispatch_async(dispatch_get_main_queue(), ^{
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, width)];
            [view addSubview:imageView];
            NSString *url = [avatarUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"headdefault"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                count ++ ;
                if (count == avatarCount) {     //图片全部下载完成
                    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 2.0);
                    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
                    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
                    UIGraphicsEndPDFContext();
                    CGImageRef imageRef = image.CGImage;
                    CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, CGRectMake(0, 0, view.frame.size.width*2, view.frame.size.height*2));
                    UIImage *ansImage = [[UIImage alloc] initWithCGImage:imageRefRect];
                    CGImageRelease(imageRefRect);
                    dispatch_async(dispatch_get_main_queue(), ^{
                        if (finished) {
                            finished(ansImage);
                        }
                });
                }
            }];
            
        }); 
     if (avatarCount == 3) {
            if (i == 2) {
                y = width + space3*2;
                x = space3;
            } else {
                x += width + space3;
            }
        } else if (avatarCount == 4) {
            if (i % 2 == 0) {
                y += width +space3;
                x = space3;
            } else {
                x += width +space3;
            }
        } else {
             if (i % 3 == 0 ) {
                 y += (width + space3);
                 x = space3;
             } else {
                 x += (width + space3);
             }
        }
    }
});

}
2,实用第三方封装好的CDDGroupAvatar

你可能感兴趣的:(iOS 群头像的简单实现)