我正在尝试使用 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];
}