简体   繁体   中英

NSCache to store images for UITableView

I have a view controller with a table view. in each cell of the table view, i have an image which is fetched from the net - but many of them have the same image. so, what i'm doing currently is storing the fetched images in a NSCache object. it happens this way:

- (void)fetchAvatarForUser:(NSString *)uid completion:(void (^)(BOOL))compBlock
{
if (!imageCache) {
    imageCache = [[NSCache alloc] init];
}
if (!avatarsFetched) {
    avatarsFetched = [[NSMutableArray alloc] initWithCapacity:0];
}

if ([avatarsFetched indexOfObject:uid] != NSNotFound) {
    // its already being fetched
} else {
    [avatarsFetched addObject:uid];
    NSString *key = [NSString stringWithFormat:@"user%@",uid];

    NSString *path = [NSString stringWithFormat:@"users/%@/avatar",uid];
    [crudClient getPath:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%@",[operation.response allHeaderFields]);
        UIImage *resImage = [UIImage imageWithData:[operation responseData]];
        [imageCache setObject:resImage forKey:key];
        compBlock(YES);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Got error: %@", error);
        compBlock(NO);
    }];
}
}

- (UIImage *)getAvatarForUser:(NSString *)uid
{
NSString *key = [NSString stringWithFormat:@"user%@",uid];
NSLog(@"Image cache has: %@",[imageCache objectForKey:key]);
return [imageCache objectForKey:key];

}

imageCache is an instance variable, and also avatarsFetched, crudClient is an AFHTTPClient object. and, in the table view:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    static NSString *CellIdentifier = @"Cell";

    PostCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[PostCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    Post *curPost = [displayedPosts objectAtIndex:indexPath.section];

    cell.nickname.text = [curPost nickname];

    UIImage *avatarImage = [self.delegateRef.hangiesCommunicator getAvatarForUser:curPost.userID];
    if (avatarImage) {
        cell.avatar.image = avatarImage;
        NSLog(@"Its not null");
    } else {
        cell.avatar.image = [UIImage imageNamed:@"20x20-user-black"];
    }
}

self.delegateRef.hangiesCommunicator returns the object (which is a retained property of the app delegate) with the imageCache as an instance variable, and the two methods at the top.

When i scroll, i see the @"Its not null" in the console, yet i don't see the fetched image but the default 20x20-user-black image. does anybody have an idea, why is this happening? what am i doing wrong?

thanks!

Your code is missing some things. I can't see where you ever call the fetchAvatarForUser:completion: method on your hangiesCommunicator and your tableView:cellForRowAtIndexPath: method is not returning the cell, so I don't think the code you posted will compile cleanly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM