2012-06-13 33 views
0

我想從使用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]; 
} 
+0

你的問題是基於一種誤解。 LDAP jpegPhoto和其他一些屬性作爲字節數組返回,而不是字符串。 – EJP

+0

我的同事和我剛剛發現。如果我被允許的話,我會稍微公佈一下這個通用代碼。 –

回答

1

試試這個代碼

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

中有許多#1類似的問題。請參考以下鏈接

UIImage to base64 String Encoding

UIImage from bytes held in NSString

+0

感謝您提供的信息,我現在已編碼,但我發現一個更大的問題。在將其從base64 NSString轉換爲NSData之前,NSString的長度表示它只有4個字符。 我會編輯我的帖子,提供更多信息。 –

+0

好的...請在你更新你的帖子後再說.. –

+0

我已經用一些代碼更新了我的帖子。 –

0

(由於已經公佈了正從LDAP圖像數據沒有工作的代碼,我想添加這個答案對未來遊客的利益。)

缺少的部分在讀二進制數據到NSData對象而不是NSString,如果您的二進制數據可能包含NULL(零)值(如圖像或GUID)。

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); 
     }; 
... 
}