0

iOS openLDAP フレームワークを使用して、openLDAP サーバーから LDAP "jpegPhoto" 属性を取得しようとしています。フレームワークは、データを NSString の辞書としてプルします。

「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];
}
4

2 に答える 2

1

このコードを試してください

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

Stackoverflow にも同様の質問が多数あります。以下のリンクを参照してください。

UIImage から base64 文字列へのエンコード

NSString に保持されているバイトからの UIImage

于 2012-06-17T05:39:35.473 に答える
0

(LDAPから画像データを取得するための実用的なコードが投稿されていないため、将来の訪問者のためにこの回答を追加したいと思いました。)

イメージや GUID などの NULL (ゼロ) 値を含む可能性のあるバイナリ データが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);
        };
...
}
于 2014-11-20T20:45:33.823 に答える