IM多人聊天群聊头像合成方法
群聊中将多个成员的头像合成为群聊的头像:
ViewController.m
//
// ViewController.m
// imageMerge
//
// Created by xxx on 2017/6/12.
// Copyright © 2017年 xxxx. All rights reserved.
//
#import "ViewController.h"
#import "UIImageView+imageMerge.h"
#define DeviceWidth [UIScreen mainScreen].bounds.size.width
#define DeviceHeight [UIScreen mainScreen].bounds.size.height
const CGFloat iconViewSideLength = 100.0f;
@interface ViewController ()
{
CGFloat _beginningY;
CGFloat _iconViewMargin;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_iconViewMargin = (DeviceWidth - iconViewSideLength * 3) / 4;
_beginningY = (DeviceHeight - iconViewSideLength * 3 - _iconViewMargin * 2) / 2;
[self createiconViews];
}
- (void)createiconViews {
int maxRow = 3;
int maxColumn = 3;
for (int i = 0; i < 8; i++)
{
int row = floor((float)i / maxRow);
int column = i % maxColumn;
CGFloat originX = _iconViewMargin + column * iconViewSideLength + column * _iconViewMargin;
CGFloat originY = _beginningY + row * iconViewSideLength + row * _iconViewMargin;
NSMutableArray * imageNamedArray = @[].mutableCopy;
for (int loop = 1; loop <= i+1; loop++)
{
[imageNamedArray addObject:[NSString stringWithFormat:@"%d", loop]];
}
UIImageView * exampleView = [UIImageView imageViewMergedByImages:imageNamedArray imageViewRadius:0.5*iconViewSideLength ];
exampleView.frame = CGRectMake(originX, originY, iconViewSideLength, iconViewSideLength);
[self.view addSubview:exampleView];
}
}
@end
//
// UIImageView+imageMerge.h
// imageMerge
//
// Created by xxx on 2017/6/12.
// Copyright © 2017年 xxxx. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIImageView (imageMerge)
/**
* 多个image合并为一个图像视图
*
* @param imageNames image数组
* @param radius 生成的UIImageView半径
*
* @return <#return value description#>
*/
+ (UIImageView *)imageViewMergedByImages:(NSArray *)imageNames imageViewRadius:(CGFloat)radius;
@end
//
// UIImageView+imageMerge.m
// imageMerge
//
// Created by xxx on 2017/6/12.
// Copyright © 2017年 xxxx. All rights reserved.
//
#import "UIImageView+imageMerge.h"
#define kMaxCount 5
@implementation UIImageView (imageMerge)
+ (UIImageView *)imageViewMergedByImages:(NSArray *)imageNames imageViewRadius:(CGFloat)radius
{
UIImageView * canvasView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 2*radius, 2*radius)];
canvasView.layer.cornerRadius = radius;
canvasView.layer.masksToBounds = YES;
canvasView.backgroundColor = [UIColor colorWithWhite:0.839 alpha:1.000];
NSArray * subImageViews = [self generatorSubViews:imageNames mageViewRadius:radius];
for (UIImageView * subImageView in subImageViews)
{
[canvasView addSubview:subImageView];
}
return canvasView;
}
+ (NSArray *)generatorSubViews:(NSArray *)imageNames mageViewRadius:(CGFloat)radius
{
NSInteger count = 0;
NSMutableArray * subImageViews = @[].mutableCopy;
NSMutableArray * subImageViewOrigins = @[].mutableCopy;
for (int loop = 0; loop < imageNames.count && loop < kMaxCount; loop++)
{
NSString * imageName = [imageNames objectAtIndex:loop];
UIImageView * subImageView = [[UIImageView alloc]init];
subImageView.image = [UIImage imageNamed:imageName];
[subImageViews addObject:subImageView];
}
count = subImageViews.count;
CGFloat originX = 0;
CGFloat originY = 0;
CGFloat smallRadius = radius;
switch (count)
{
case 1:
{
[subImageViewOrigins addObject:@{@"origin.x":@(originX),@"origin.y":@(originY)}];
break;
}
case 2:
{
smallRadius = 0.6*radius;
originX = radius-sqrt(2)*radius/2+sqrt(2)*smallRadius/2-smallRadius;
originY = originX;
[subImageViewOrigins addObject:@{@"origin.x":@(originX),@"origin.y":@(originY)}];
originX += sqrt(2)*(radius-smallRadius);
originY = originX;
[subImageViewOrigins addObject:@{@"origin.x":@(originX),@"origin.y":@(originY)}];
break;
}
case 3:
{
smallRadius = 0.5*radius;
originX = radius - smallRadius;
originY = 0;
[subImageViewOrigins addObject:@{@"origin.x":@(originX),@"origin.y":@(originY)}];
originX = radius - sqrt(3)*smallRadius/2 - smallRadius;
originY = 1.5*smallRadius;
[subImageViewOrigins addObject:@{@"origin.x":@(originX),@"origin.y":@(originY)}];
originX = radius + sqrt(3)*smallRadius/2-smallRadius;
[subImageViewOrigins addObject:@{@"origin.x":@(originX),@"origin.y":@(originY)}];
break;
}
case 4:
{
smallRadius = radius*0.45;
originX = 0.293*(radius-smallRadius);
originY = originX;
[subImageViewOrigins addObject:@{@"origin.x":@(originX),@"origin.y":@(originY)}];
originX = 1.707*(radius-smallRadius);
[subImageViewOrigins addObject:@{@"origin.x":@(originX),@"origin.y":@(originY)}];
originX = 0.293*(radius-smallRadius);
originY = 1.707*(radius-smallRadius);
[subImageViewOrigins addObject:@{@"origin.x":@(originX),@"origin.y":@(originY)}];
originX = 1.707*(radius-smallRadius);
originY = 1.707*(radius-smallRadius);
[subImageViewOrigins addObject:@{@"origin.x":@(originX),@"origin.y":@(originY)}];
break;
}
case 5:
{
smallRadius = radius*0.45;
originX = radius - smallRadius;
originY = 0;
[subImageViewOrigins addObject:@{@"origin.x":@(originX),@"origin.y":@(originY)}];
originX = (radius - smallRadius)*0.049;
originY = smallRadius;
[subImageViewOrigins addObject:@{@"origin.x":@(originX),@"origin.y":@(originY)}];
originX = (radius - smallRadius)*1.951;
originY = smallRadius;
[subImageViewOrigins addObject:@{@"origin.x":@(originX),@"origin.y":@(originY)}];
originX = radius - (radius-smallRadius)*0.588-smallRadius;
originY = radius + (radius-smallRadius)*0.809-smallRadius;
[subImageViewOrigins addObject:@{@"origin.x":@(originX),@"origin.y":@(originY)}];
originX = radius + (radius-smallRadius)*0.588-smallRadius;
originY = radius + (radius-smallRadius)*0.809-smallRadius;
[subImageViewOrigins addObject:@{@"origin.x":@(originX),@"origin.y":@(originY)}];
break;
}
default:
break;
}
for (int loop = 0; loop < subImageViews.count; loop++)
{
UIImageView * imageView = [subImageViews objectAtIndex:loop];
NSDictionary * originDict = [subImageViewOrigins objectAtIndex:loop];
imageView.frame = CGRectMake([originDict[@"origin.x"] floatValue], [originDict[@"origin.y"] floatValue], 2*smallRadius, 2*smallRadius);
[[imageView layer] setCornerRadius:smallRadius];
[[imageView layer] setMasksToBounds:YES];
}
return subImageViews;
}
@end