群组头像拼接
来源:互联网 发布:好的哲学书籍推荐知乎 编辑:程序博客网 时间:2024/05/18 02:51
利用画板实现群组头像图片拼接
附参考:StitchingImage
该库也可实现群组头像,是通过添加多个UIImageVIew拼接,返回的是一个UIImageView,感觉不是很方便,于是博主就照葫芦画瓢实现一个返回是UIImage的拼接头像
不多说,上代码:
//配合SDWebImage根据图片URL获取图片(有缓存则获取缓存的图片,没有则网络链接获取)+ (UIImage *)imageWithUrlString:(NSString *)imageUrl{ UIImageView * imageView = [[UIImageView alloc] init]; NSURL * url = [NSURL URLWithString:imageUrl]; [imageView sd_setImageWithURL:url placeholderImage:nil]; SDWebImageManager *manager = [SDWebImageManager sharedManager]; BOOL existBool = [manager diskImageExistsForURL:url];//判断是否有缓存 UIImage * image; if (existBool) { image = [[manager imageCache] imageFromDiskCacheForKey:url.absoluteString]; }else{ NSData *data = [NSData dataWithContentsOfURL:url]; image = [UIImage imageWithData:data]; } return image;}//传入图片数组即可(如果是读取链接图片可配合SDWebImage使用上面的方法获取图片再添加入数组)//拼接完成返回一张图片+ (UIImage *) combine:(NSArray<UIImage *> *)images{ CGSize offScreenSize = CGSizeMake(200, 200); UIGraphicsBeginImageContext(offScreenSize); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [[UIColor lightGrayColor] CGColor]);//图片背景色 CGContextFillRect(context, CGRectMake(0, 0, 200, 200)); //确定拼接图片的宽度 CGFloat imageWidth = [self generateImageWidthWithImageCount:images.count]; switch (images.count) { case 2: { CGFloat row_1_origin_y = (200 - imageWidth) / 2; [self generatorMatrix:images beginOriginY:row_1_origin_y]; } break; case 3: { CGFloat row_1_origin_y = (200 - imageWidth * 2) / 3; UIImage* image_1 = images[0]; CGRect rect_1 = CGRectMake((200 - imageWidth) / 2, row_1_origin_y, imageWidth, imageWidth); [image_1 drawInRect:rect_1]; [self generatorMatrix:images beginOriginY:row_1_origin_y + imageWidth + 10]; } break; case 4: { CGFloat row_1_origin_y = (200 - imageWidth * 2) / 3; [self generatorMatrix:images beginOriginY:row_1_origin_y]; } break; case 5: { CGFloat row_1_origin_y = (200 - imageWidth * 2 - 10) / 2; UIImage* image_1 = images[0]; CGRect rect_1 = CGRectMake((200 - 2 * imageWidth - 10) / 2, row_1_origin_y, imageWidth, imageWidth); [image_1 drawInRect:rect_1]; UIImage* image_2 = images[1]; CGRect rect_2 = CGRectMake(rect_1.origin.x + imageWidth + 10, row_1_origin_y, imageWidth, imageWidth); [image_2 drawInRect:rect_2]; [self generatorMatrix:images beginOriginY:row_1_origin_y + imageWidth + 10]; } break; case 6: { CGFloat row_1_origin_y = (200 - imageWidth * 2 - 10) / 2; [self generatorMatrix:images beginOriginY:row_1_origin_y]; } break; case 7: { CGFloat row_1_origin_y = (200 - imageWidth * 3) / 4; UIImage* image_1 = images[0]; CGRect rect_1 = CGRectMake((200 - imageWidth) / 2, row_1_origin_y, imageWidth, imageWidth); [image_1 drawInRect:rect_1]; [self generatorMatrix:images beginOriginY:row_1_origin_y + imageWidth + 10]; } break; case 8: { CGFloat row_1_origin_y = (200 - imageWidth * 3) / 4; UIImage* image_1 = images[0]; CGRect rect_1 = CGRectMake((200 - 2 * imageWidth - 10) / 2, row_1_origin_y, imageWidth, imageWidth); [image_1 drawInRect:rect_1]; UIImage* image_2 = images[1]; CGRect rect_2 = CGRectMake(rect_1.origin.x + imageWidth + 10, row_1_origin_y, imageWidth, imageWidth); [image_2 drawInRect:rect_2]; [self generatorMatrix:images beginOriginY:row_1_origin_y + imageWidth + 10]; } break; case 9: { CGFloat row_1_origin_y = (200 - imageWidth * 3) / 4; [self generatorMatrix:images beginOriginY:row_1_origin_y]; } break; default: break; } UIImage* imagez = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return imagez;}+ (void)generatorMatrix:(NSArray *)images beginOriginY:(CGFloat)beginOriginY { int count = (int)images.count; int cellCount; int maxRow; int maxColumn; int ignoreCountOfBegining; if (count <= 4) { maxRow = 2; maxColumn = 2; ignoreCountOfBegining = count % 2; cellCount = 4; } else { maxRow = 3; maxColumn = 3; ignoreCountOfBegining = count % 3; cellCount = 9; } CGFloat imageWidth = [self generateImageWidthWithImageCount:images.count]; for (int i = 0; i < cellCount; i++) { if (i > images.count - 1) break; if (i < ignoreCountOfBegining) continue; int row = floor((float)(i - ignoreCountOfBegining) / maxRow); int column = (i - ignoreCountOfBegining) % maxColumn; CGFloat origin_x = 10 + imageWidth * column + 10 * column; CGFloat origin_y = beginOriginY + imageWidth * row + 10 * row; CGRect rect = CGRectZero; rect = CGRectMake(origin_x, origin_y, imageWidth, imageWidth); [images[i] drawInRect:rect]; }}+ (CGFloat)generateImageWidthWithImageCount:(NSInteger)count { CGFloat sideLength = 0.0f; if (count >=2 && count <=4) { sideLength = (200 - 10 * 3) / 2; } else { sideLength = (200 - 10 * 4) / 3; } return sideLength;}
效果如下图:
0 0
- 群组头像拼接
- 群组头像合成控件
- android 群组头像的显示(仿QQ群组)
- android 群组头像的显示(仿QQ群组)
- Android:实现IM多人员组合的群组头像
- android 仿微信群组头像效果
- Android 仿微信群组头像合成
- 自定义View-仿微信群组头像
- 关于QQ群头像以及微信讨论组头像的工具类
- 高仿QQ讨论组头像拼图
- Android 仿QQ讨论组头像
- 仿微信群组头像组合边框实现
- Swift - QQ讨论组头像的实现 (多人聊天的组合头像)
- UICollectionView分组-组头处理
- uitableview组头悬浮问题
- 拼接
- 拼接
- 数组指针和数组头指针
- Setting up a Linux build environment
- POJ 2083 Fractal(递归)
- 终于放假了!!
- WKWebview相关知识点记录
- 小人儿的笔记(AngularJS)--04
- 群组头像拼接
- 最全免费CDN公共库——网站提速 .
- 设计模式在综合前置中的运用(三)--建造者模式
- 0001-数据库系统——(总述)
- Sparkstreaming中InputDStream的详解(源码)
- dropdownlist
- 拥有博客的第一天(心情文,非技术文)
- js实时监听输入框的值
- 关于弹出层的总结