找到你要的答案

Q:Loading JSON image Fast

Q:图像的快速加载JSON

I am developing an iOS App. And fetch images through JSON and show on tableview. Using this method dispatch_async(kBgQueue, ^{... and image show on table view but all image show on one cell and automatically recycling. Image can be change after some time and also using the placeholder image. To Show Image using two string one string Can use for image name and second string can be use for URL. Then concat the string to show image thats same problem in collection view. Any other method to show image fast in tableview and collection view.
My code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {
        mycell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
        UILabel *mylabel = (UILabel *)[mycell viewWithTag:100];
        NSDictionary *dic=[str objectAtIndex:indexPath.row];
        GRLanguage *sharedManager = [GRLanguage sharedManager];
        if ([sharedManager.LanguageCode isEqual:@"hi"]) {
            mylabel.text=[dic objectForKey:@"hindi_name"];
        }else if ([sharedManager.LanguageCode isEqual:@"pa"])
        {
            mylabel.text=[dic objectForKey:@"punjabi_name"];
        }else
        {
            mylabel.text=[dic objectForKey:@"name"];
        }


        [mycell setBackgroundColor:[UIColor clearColor]];
        UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back copy.png"]];
        self.tableview.backgroundView =img;


        dispatch_async(kBgQueue, ^{     //Using For Lazy Loading
            UIImageView *imagetlt = (UIImageView *)[mycell viewWithTag:1];
            NSString *image =[dic objectForKey:@"image_path"];
            NSString *path=@"http://webapp.opaxweb.net/images/";
            path=[path stringByAppendingString:image];
            NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:path]];
            if (image == nil) {
                 [imagetlt setImage:[UIImage imageNamed:@"rel_circle.png"]];
            }else
            {
                dispatch_async(dispatch_get_main_queue(), ^{
                  //  UITableViewCell*img = (id) [tableView cellForRowAtIndexPath:indexPath];
                    imagetlt.image = [UIImage imageWithData: imageData];
                });

            }


    });
     return mycell;
}

I am developing an iOS App. And fetch images through JSON and show on tableview. Using this method dispatch_async(kBgQueue, ^{... and image show on table view but all image show on one cell and automatically recycling. Image can be change after some time and also using the placeholder image. To Show Image using two string one string Can use for image name and second string can be use for URL. Then concat the string to show image thats same problem in collection view. Any other method to show image fast in tableview and collection view.
My code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {
        mycell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
        UILabel *mylabel = (UILabel *)[mycell viewWithTag:100];
        NSDictionary *dic=[str objectAtIndex:indexPath.row];
        GRLanguage *sharedManager = [GRLanguage sharedManager];
        if ([sharedManager.LanguageCode isEqual:@"hi"]) {
            mylabel.text=[dic objectForKey:@"hindi_name"];
        }else if ([sharedManager.LanguageCode isEqual:@"pa"])
        {
            mylabel.text=[dic objectForKey:@"punjabi_name"];
        }else
        {
            mylabel.text=[dic objectForKey:@"name"];
        }


        [mycell setBackgroundColor:[UIColor clearColor]];
        UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back copy.png"]];
        self.tableview.backgroundView =img;


        dispatch_async(kBgQueue, ^{     //Using For Lazy Loading
            UIImageView *imagetlt = (UIImageView *)[mycell viewWithTag:1];
            NSString *image =[dic objectForKey:@"image_path"];
            NSString *path=@"http://webapp.opaxweb.net/images/";
            path=[path stringByAppendingString:image];
            NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:path]];
            if (image == nil) {
                 [imagetlt setImage:[UIImage imageNamed:@"rel_circle.png"]];
            }else
            {
                dispatch_async(dispatch_get_main_queue(), ^{
                  //  UITableViewCell*img = (id) [tableView cellForRowAtIndexPath:indexPath];
                    imagetlt.image = [UIImage imageWithData: imageData];
                });

            }


    });
     return mycell;
}
answer1: 回答1:

You have a severe problem. You create a cell, then you try to load an image asynchrously and try to store it in the cell. That cannot work. Cells will be reused. By the time the image arrives, the cell may be displaying completely different data.

Here's what you do: You keep images returned from URLs cached in memory or stored in files. In cellForRowAtIndexPath you try retrieving a cached image and store that, or you store a placeholder image and start the asynchronous download. When the download finishes, you put the image into your cache and call reloadRowsAtIndexPaths: so that the row will be reloaded.

What's even better, if items can change their row while the asynchronous download is running (because the user adds or deletes rows, or sorting order is changed and so on), when the asynchronous download ends, you figure out which row contains that item (which might not be the row containing it when the download started), and call reloadRowsAtIndexPaths: for that row.

你有一个严重的问题。你创建了一个细胞,然后你尝试asynchrously装载图像并试图把它储存在细胞。不能工作。细胞将被重复使用。当图像到达时,单元格可以显示完全不同的数据。

以下是你做的:你保持图像返回的网址缓存在内存中或存储在文件中。在cellForRowAtIndexPath你尝试缓存图像检索和存储,或者你店里的占位符图像并启动异步下载。当下载完成后,你把图片放入缓存和呼叫reloadrowsatindexpaths:使行将重装上阵。

更好的是,如果项目能在异步下载正在改变他们的行(因为用户添加或删除行,或排序顺序变化等),当异步下载完毕,你找出哪些行包含项目(可能不包含它的下载开始时,行),叫reloadrowsatindexpaths:那行。

ios  json  uitableview