我想從使用iOS openLDAP框架的openLDAP服務器拉取LDAP「jpegPhoto」屬性。該框架將數據作爲NSStrings的字典提取。我如何將LDAP「jpegPhoto」轉換爲NSString到UIImageView
我需要將「jpegPhoto」(它也是base64編碼的)的NSString轉換爲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];
}
你的問題是基於一種誤解。 LDAP jpegPhoto和其他一些屬性作爲字節數組返回,而不是字符串。 – EJP
我的同事和我剛剛發現。如果我被允許的話,我會稍微公佈一下這個通用代碼。 –