0

I am trying to pull an LDAP "jpegPhoto" attribute from an openLDAP server using a iOS openLDAP framework. The framework pulls the data as a dictionary of NSStrings.

I need to convert the NSString of "jpegPhoto" (which also appears to be base64 encoded) to UIImage, with the end result being that I display the jpegPhoto as the user's image when they login.

More Info:

-(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];
}
user207421
  • 305,947
  • 44
  • 307
  • 483
Phillip Boushy
  • 445
  • 1
  • 4
  • 14
  • Your question is based on a misconception. An LDAP jpegPhoto, and some other attributes, are returned as byte arrays, not strings. – user207421 Jun 24 '12 at 02:00
  • My co-worker and I just found that. If I'm allowed to I'll post the generalized code for this in a little bit. – Phillip Boushy Jun 26 '12 at 19:49

2 Answers2

1

Try this code

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

There are many similar questions in Stackoverflow.. Please refer the following links

UIImage to base64 String Encoding

UIImage from bytes held in NSString

Community
  • 1
  • 1
Dilip Rajkumar
  • 7,006
  • 6
  • 60
  • 76
  • Thank you for the information, I've now coded that in, but I'm finding a larger issue. Prior to converting it from the base64 NSString to NSData, the NSString's length says it is only 4 characters. I'll edit my post with more information. – Phillip Boushy Jun 23 '12 at 23:18
  • Thank you for all your help! I solved the issue with the help of our other programmer. I was pulling the size of the variable that stored the data originally. When I was supposed to use another OpenLDAP method to get the size of the jpegPhoto. I need to check with my bosses if I can post the code or if its under confidentiality. I think it's a great thing for everyone to have. – Phillip Boushy Jun 26 '12 at 19:45
  • Since your comment did answer the original question I'm going to mark it as correct. – Phillip Boushy Jun 26 '12 at 19:52
  • Sorry Phillip I missed your update.. Glad you solved your problem.. Have a great day.. – Dilip Rajkumar Jun 27 '12 at 04:32
0

(Since there has been no working code posted for getting the image data from LDAP, I wanted to add this answer for the benefit of future visitors.)

The missing piece was reading the binary data into an NSData object rather than an NSString when you have binary data that might contain NULL (zero) values within it, such as images or GUIDs.

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);
        };
...
}
picciano
  • 22,341
  • 9
  • 69
  • 82