繁体   English   中英

如何将LDAP“ jpegPhoto”转换为NSString到UIImageView

[英]How do I convert an LDAP “jpegPhoto” to NSString to UIImageView

我正在尝试使用iOS openLDAP框架从openLDAP服务器中提取LDAP“ jpegPhoto”属性。 该框架将数据提取为NSStrings的字典。

我需要将“ jpegPhoto”的NSString(也似乎是base64编码)转换为UIImage,最终结果是我在登录时将jpegPhoto显示为用户的图像。

更多信息:

-(NSDictionary *)doQuery:(NSString *)query:(NSArray *)attrsToReturn {
    ...
    while(attribute){
        if ((vals = ldap_get_values_len(ld, entry, attribute))){
            for(int i = 0; vals[i]; i++){
                //Uncomment if you want to see all the values.
                //NSLog(@"%s: %s", attribute, vals[i]->bv_val);
                if ([resultSet objectForKey:[NSString stringWithFormat:@"%s",attribute]] == nil){
                    [resultSet setObject:[NSArray arrayWithObject:[NSString stringWithFormat:@"%s",vals[i]->bv_val]] forKey:[NSString stringWithFormat:@"%s",attribute]];
                }else{
                    NSMutableArray *array = [[resultSet objectForKey:[NSString stringWithFormat:@"%s",attribute]] mutableCopy];
                    [array addObject:[NSString stringWithFormat:@"%s",vals[i]->bv_val]];
                    [resultSet setObject:array forKey:[NSString stringWithFormat:@"%s",attribute]];
                }
            }
            ldap_value_free_len(vals);
        };
        ldap_memfree(attribute);
        attribute = ldap_next_attribute(ld, entry, ber);
    };
    ...
}

-(UIIMage *)getPhoto{
    NSString *query = [NSString stringWithFormat:@"(uid=%@)",self.bindUsername];
    NSArray *attrsToReturn = [NSArray arrayWithObjects:@"cn",@"jpegPhoto", nil];
    NSDictionary *rs = [self doQuery:query:attrsToReturn];
    NSString *photoString = [[rs objectForKey:@"jpegPhoto"] objectAtIndex:0];
    NSLog(@"The photoString is: %i %@",[photoString length],@"characters long"); //returns 4
    NSData *photoData = [NSData dataWithBase64EncodedString:photoString];
    UIImage *userPhoto = [UIImage imageWithData:photoData];
    return userPhoto;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.studentNameLabel.text = [NSString stringWithFormat:@"Hi %@!",[self.ldap getFullName]];
    self.studentPhotoImage.image = [self.ldap getPhoto];
    [self checkForProctor];
}

试试这个代码

NSData *dataObj = [NSData dataWithBase64EncodedString:beforeStringImage];
UIImage *beforeImage = [UIImage imageWithData:dataObj];

Stackoverflow中也有许多类似的问题。.请参考以下链接

UIImage到base64字符串编码

UIImage来自NSString中保存的字节

(由于没有发布用于从LDAP获取图像数据的工作代码,因此我想添加此答案,以便将来的访问者受益。)

当您的二进制数据中可能包含NULL(零)值(例如图像或GUID)时,缺少的部分是将二进制数据读取到NSData对象而不是NSString

value = [NSData dataWithBytes:vals[0]->bv_val length:vals[0]->bv_len];

+ (NSArray *)searchWithBaseDN:(const char *)baseDN andFilter:(const char *)filter andScope:(int)scope {
    ...
    while(entry)
    {
        // create a dictionary to hold attributes
        NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];

        attribute = ldap_first_attribute(ld, entry, &ber);
        while(attribute)
        {
            if ((vals = ldap_get_values_len(ld, entry, attribute)))
            {
                if (ldap_count_values_len(vals) > 1) {
                    NSMutableArray *values = [[NSMutableArray alloc] init];
                    for(int i = 0; vals[i]; i++) {
                        [values addObject:[NSString stringWithUTF8String:vals[i]->bv_val]];
                    }
                    [dictionary setObject:values forKey:[NSString stringWithUTF8String:attribute]];
                } else {
                    NSObject *value = nil;
                    if (strcmp(attribute, "thumbnailPhoto") == 0 || strcmp(attribute, "objectGUID") == 0) {
                        value = [NSData dataWithBytes:vals[0]->bv_val length:vals[0]->bv_len];
                    } else {
                        value = [NSString stringWithFormat:@"%s", vals[0]->bv_val];
                    }
                    [dictionary setObject:value forKey:[NSString stringWithUTF8String:attribute]];
                }


                ldap_value_free_len(vals);
            };
            ldap_memfree(attribute);
            attribute = ldap_next_attribute(ld, entry, ber);
        };
...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM