智慧服务,成就美好体验 项目咨询

主页 > 服务与支持 > 开发平台 > 客户端SDK参考 > iOS Native SDK > 企业通讯录 设置和获取联系人头像

入门使用

设置和获取联系人头像

更新时间:2019-11-20

描述

用户可以更改自己的头像,使用的头像可以是系统头像也可以是自定义头像。同时用户可以获取头像,在获取头像时服务器会推送获取头像的回调消息给用户,从服务器上得到指定联系人的头像信息。

已完成组件的初始化和登录。

业务流程

设置系统头像

图1 设置系统头像流程 
  1. UI调用tsdk_set_system_icon()设置系统头像。
    说明: 
    1. 参数icon_id是指系统默认头像图片文件的图片名。图片名一般是数字。这些图片文件保存在用户的本地文件夹中。这些图片不需要从服务器上获取。
    2. 禁止修改系统默认头像图片的文件名,以免造成icon_id对应的图片文件与其他用户不一致,导致界面显示用户系统默认头像不一致。
    3. 头像大小建议不要超过2M,否则可能影响UI性能。

    代码示例:

    - (void)setSystemHead:(int)sysIconID withCmpletion:(void(^)(BOOL result))completionBlock 
    {   
        TSDK_RESULT set_sys_result = tsdk_set_system_icon((TSDK_UINT32)sysIconID);    
        BOOL result = set_sys_result == TSDK_SUCCESS;    
        if (completionBlock) 
        {        
            completionBlock(result);    
        }    
        if (result) 
        {        
            [self setHeadID:[NSString stringWithFormat:@"%d", sysIconID]];    
        }
    }
     

 

设置自定义头像

图2 设置自定义头像流程 
  1. UI调用tsdk_set_user_def_icon()设置自定义头像。
    说明: 
    1. 设置自定义头像需注意:在参数TSDK_S_ICON_INFO自定义头像信息里需要设置头像的路径,并且头像一般需要选择为:小头像52*52、中头像120*120和大头像320*320三种。其中具体大小应根据服务器要求来定;
    2. 图片的格式不支持TIFF格式,推荐使用png格式;
    3. 图片的大小服务器没有限制,产品可以根据实际的需要进行限制,建议图片大小最大值为2MB;
    4. 若返回成功,则返回设置成功和修改时间,否则返回相应的错误码。

    代码示例:

    - (void)setHeadImage:(UIImage *)image completion:(void(^)(BOOL result, NSString *headID))completionBlock 
    {    
    //自定义头像接口需要上传三种尺寸的图片:52x52   120x120   320x320     
        NSData *minImg = [self imgWithSize:SIZE52 image:image];    
        NSData *midImg = [self imgWithSize:SIZE120 image:image];    
        NSData *maxImg = [self imgWithSize:SIZE320 image:image];    
        NSString *path = NSTemporaryDirectory();    
        NSString *iconPathMinImg = [path stringByAppendingPathComponent:@"minImg"];    
        NSString *iconPathMidImg = [path stringByAppendingPathComponent:@"midImg"];    
        NSString *iconPathMaxImg = [path stringByAppendingPathComponent:@"maxImg"];    
        [minImg writeToFile:iconPathMinImg atomically:YES];    
        [midImg writeToFile:iconPathMidImg atomically:YES];    
        [maxImg writeToFile:iconPathMaxImg atomically:YES];        
        TSDK_S_ICON_INFO* icon_info = (TSDK_S_ICON_INFO*)malloc(sizeof(TSDK_S_ICON_INFO));    
        memset(icon_info, 0, sizeof(TSDK_S_ICON_INFO));    
        strcpy(icon_info->small_icon_path, [iconPathMinImg UTF8String]);    
        strcpy(icon_info->medium_icon_path, [iconPathMidImg UTF8String]);    
        strcpy(icon_info->large_icon_path, [iconPathMaxImg UTF8String]);    
        TSDK_CHAR *modifyTime = (TSDK_CHAR *)malloc(16);    
        memset_s(modifyTime, 16, 0, 16);    
        TSDK_UINT32 length = 16;        
        TSDK_RESULT ret_set_def = tsdk_set_user_def_icon(icon_info, modifyTime, &length);    
        free(icon_info);    
    // 出参modifyTime时间戳,作为联系人headId    
        NSString *mTime = [NSString stringWithUTF8String:modifyTime];    
        DDLogInfo(@"set image ret: %d modify time: %@", length, mTime);    
        BOOL result = ret_set_def == TSDK_SUCCESS;    
        if (completionBlock) 
        {        
            completionBlock(result, mTime);    
        }    
        if (result) 
        {        
            [self setHeadID:mTime];    
        }
    }
     

 

获取联系人头像

图3 搜索联系人头像流程 
  1. UI调用tsdk_get_user_icon()搜索联系人头像。
    说明: 

    传入的参数TSDK_S_GET_ICON_PARAM即为获取头像请求参数,需要填写查询的帐户以及序列号。

    代码示例:
    - (void)loadPersonHeadIconWithAccount:(NSString *)account 
    {    
        TSDK_S_GET_ICON_PARAM *iconParam = (TSDK_S_GET_ICON_PARAM *)malloc(sizeof(TSDK_S_GET_ICON_PARAM));    
        memset(iconParam, 0, sizeof(TSDK_S_GET_ICON_PARAM));   
        if (account.length > 0 && account != nil) 
        {        
            strcpy(iconParam->account, [account UTF8String]);    
        }    
        iconParam->seq_no = rand();    
        TSDK_RESULT result = tsdk_get_user_icon(iconParam);    
        DDLogInfo(@"tsdk_get_user_icon result: %d", result);    
        free(iconParam);
    }
     
  2. SDK收到服务器下发的获取联系人头像结果,下发TSDK_E_EADDR_EVT_GET_ICON_RESULT的事件,通知给UI所查询的联系人头像内容。
    说明: 

    查询头像的结果信息包括操作结果,查询序号,系统头像id(用户设置的是系统头像)或者自定义头像文件路径(用户设置的是自定义头像)。

    代码示例:
    case TSDK_E_EADDR_EVT_GET_ICON_RESULT: 
    {                
        DDLogInfo(@"TSDK_E_EADDR_EVT_GET_ICON_RESULT");                
        BOOL result = notification.param1 == TSDK_SUCCESS;                
        if (!result) 
        {                     
            DDLogError(@"TSDK_E_EADDR_EVT_GET_ICON_RESULT,error:%@",[NSString stringWithUTF8String:(TSDK_CHAR *)notification.data]);                    
            return;                
        }                                
        TSDK_S_GET_ICON_RESULT *getIconResult = (TSDK_S_GET_ICON_RESULT *)notification.data;                
        int sysIconID = getIconResult->icon_id;                
        NSString *acIconFile = [NSString stringWithUTF8String:getIconResult->icon_path];                                
        NSDictionary *resultInfo = @{
            TUP_CONTACT_EVENT_RESULT_KEY : [NSNumber numberWithBool:result],                                             
            TUP_SYS_ICON_ID_KEY : [NSString stringWithFormat:@"%d", sysIconID],                                             
            TUP_ICON_FILE_KEY : [NSString stringWithFormat:@"%@%@", ICON_PATH, acIconFile]
        };                
        [self respondsContactDelegateWithType:CONTACT_E_SEARCH_GET_ICON_RESULT result:resultInfo];            
    }               
     break;
     

注意事项

无。