微信頭像拼接

這兩天剛好項(xiàng)目上有一個(gè)頭像拼接的需求,想必做即時(shí)通訊的很多都有這樣的需求吧,所以就研究了一下微信的規(guī)則實(shí)現(xiàn)了一下。
效果如下


QQ20160929-0@2x.png

代碼

/**
頭像拼接
 @param array        圖片數(shù)組
 @param size         圖片大小
 @param cornerRadius 圓角半徑

 @return <#return value description#>
 */
-(UIImage *)imageCompose:(NSArray *)array size:(CGSize)size cornerRadius:(CGFloat)cornerRadius
{
    if(array.count)
    {
        //開啟一個(gè)圖片上下文
        UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale) ;
        
        //添加裁剪路徑
        UIBezierPath *clippath=[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, size.width, size.height) cornerRadius:cornerRadius];
        [clippath addClip];
        
        //繪制背景矩形
        UIBezierPath *path=[UIBezierPath bezierPathWithRect:CGRectMake(0, 0, size.width, size.height)];
        [[UIColor lightGrayColor] setFill];
        [path fill];
        
       
        NSInteger count=array.count;
        if(count>9)
            count=9;
        
        //最大列數(shù)
        NSInteger max_col=3;
        //最大行數(shù)
        NSInteger max_row=3;
        //列數(shù)
        NSInteger column=ceil(count/2.0);
        if(column>max_col)
            column=max_col;
        else if (column<1)
            column=1;
        //行數(shù)
        NSInteger row=ceil(count/(CGFloat)column);
        if(row>max_row)
            row=max_row;
        
        
        
        if(row<1)
            row=1;
        
        //每個(gè)頭像間的間距
        CGFloat margin=2;
        CGFloat imgsize=(size.width-(MAX(column, row)+1)*margin)/MAX(column, row);
        
        //每一行的列數(shù)
        NSInteger rowcount=ceil(count/(CGFloat)row);
        NSInteger k=0;
        for (int i =0; i<row; i++) {
            
            CGFloat rowmargin=(size.height-imgsize*row-margin*(MAX(row-1, 1)))/(CGFloat)row;
            //每一行的y值
            CGFloat y=i*imgsize+(i+1)*rowmargin;
            if(i>0)
                y=rowmargin+i*imgsize+i*margin;
            if(count<row*column&&i==0)
            {
                //第一行的列數(shù)
                NSInteger count1=rowcount-(row*column-count);
                CGFloat margin1=(size.width-imgsize*count1-margin*MAX(count1-1, 1))/2.0;
                for (int j=0; j<count1; j++) {
                    UIImage *img=array[k];
                    CGFloat x=margin1;
                    if(j>0)
                        x=margin1+j*imgsize+j*margin;
                    [img drawInRect:CGRectMake(x,y, imgsize, imgsize)];
                    k++;
                }
            }
            else
            {
                CGFloat margin1=(size.width-imgsize*rowcount)/(rowcount+1);
                for (int j=0; j<rowcount; j++) {
                    UIImage *img=array[k];
                    [img drawInRect:CGRectMake(j*imgsize+(j+1)*margin1, y, imgsize, imgsize)];
                    k++;
                }
            }

           
        }
        UIImage *newimge=UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newimge;

    }
    return nil;
}
  • 調(diào)用
 NSMutableArray *imgs=[NSMutableArray array];
    [imgs addObject:[UIImage imageNamed:@"al1.jpg"]];
    [imgs addObject:[UIImage imageNamed:@"al2.jpg"]];
    [imgs addObject:[UIImage imageNamed:@"al3.jpg"]];
    [imgs addObject:[UIImage imageNamed:@"al4.jpg"]];
    [imgs addObject:[UIImage imageNamed:@"al5.jpg"]];
    [imgs addObject:[UIImage imageNamed:@"al6.jpg"]];
    [imgs addObject:[UIImage imageNamed:@"al7.jpg"]];
    [imgs addObject:[UIImage imageNamed:@"al8.jpg"]];
    [imgs addObject:[UIImage imageNamed:@"al9.jpg"]];
    UIImage *img=[self imageCompose:imgs size:CGSizeMake(100, 100) cornerRadius:10];
    self.imgv.image=img;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,940評論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,188評論 4 61
  • 老爸自從在老公“善意的謊言”下戒煙成功之后,老爸每天都是兜里揣著各種含片,老媽提著各種小零食茶水緊跟其后,繼續(xù)當(dāng)著...
    睡_蓮閱讀 391評論 0 2
  • The ATA Code of Ethics美國塔羅協(xié)會之倫理戒律 1. I will serve the bes...
    78度微學(xué)院閱讀 386評論 0 0
  • 一個(gè)人包場看了電影。 看著湖光山色,腦海中出現(xiàn)了另外一個(gè)電影的臺詞。 鄭微對著兩邊的大山說,愛一個(gè)人就像愛大山,愛...
    失眠歡歌閱讀 521評論 0 0

友情鏈接更多精彩內(nèi)容