简体   繁体   English

NSCache存储UITableView的图像

[英]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. 所以,我目前正在做的是将获取的图像存储在NSCache对象中。 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. imageCache是​​一个实例变量,也是avatarsFetched,crudClient是一个AFHTTPClient对象。 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. self.delegateRef.hangiesCommunicator返回对象(这是app委托的保留属性),imageCache作为实例变量,顶部有两个方法。

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. 当我滚动时,我在控制台中看到@“它不为空”,但我没有看到提取的图像,而是默认的20x20用户黑色图像。 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. 我无法在你的hangiesCommunicator上调用fetchAvatarForUser:completion:方法,你的tableView:cellForRowAtIndexPath:方法没有返回单元格,所以我认为你发布的代码不会干净地编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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