简体   繁体   English

在Dispatch_queue的UITableView单元格中获取相同的图像

[英]Getting same images in UITableView cell in Dispatch_queue

I am adding images from server. 我正在从服务器添加图像。 I am using NSMutableArray and custom UITableViewCell. 我正在使用NSMutableArray和自定义UITableViewCell。 Problem : When I run the project. 问题:运行项目时。 UITableViewCell displaying same images. UITableViewCell显示相同的图像。 I think it refreshing cell. 我认为它令人耳目一新。 How can I fix that issue? 我该如何解决该问题?

Below dispatch method I used, 下面是我使用的调度方法

   dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        //this will start the image loading in bg
        dispatch_async(concurrentQueue, ^{
            NSError *nserror = nil;
            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString: [imagesArray2 objectAtIndex:indexPath.row]]options:NSDataReadingUncached error:&nserror];

            //this will set the image when loading is finished
            dispatch_async(dispatch_get_main_queue(), ^{
                if (nserror)
                {
                    [cell.imgview setImage:[UIImage imageNamed:@"placeholderimage.png"]];
                }
                else
                {
                    [cell.imgview setImage:[UIImage imageWithData:imageData]];
                }
                [mytablview reloadData];
            });
        });

Thanks for any help. 谢谢你的帮助。

This doesn't really answer your question, but it may help you change the direction you're heading in. 这并不能真正回答您的问题,但可以帮助您改变前进的方向。

When you finish loading the image, the cell may have already started loading a different image because UITableView s reuse their cells. 完成加载图像后,单元格可能已经开始加载其他图像,因为UITableView重复使用了它们的单元格。 The operation of asynchronously loading the image and setting it once it's done doesn't get cancelled and so you're going to get incorrect images popping up all over the shop, since there's no guarantee of which image loading finishes first. 异步加载图像并在完成后对其进行设置的操作不会被取消,因此您将在整个商店中弹出不正确的图像,因为无法保证首先完成哪个图像加载。

Please see my comment regarding AFNetworking , which I highly recommend that you use, but if you go down a different path then you should be using some kind of NSOperation subclass for loading the images. 请参阅我对AFNetworking的评论,我强烈建议您使用它,但是如果您走的路不对 ,则应该使用某种NSOperation子类来加载图像。 That way you can cancel the operations when the cells are reused and you can then safely load a different image for that cell. 这样,您可以在重用单元格时取消操作,然后可以安全地为该单元格加载其他图像。

Just reset cell imageView, before loading new image. 在加载新图像之前,只需重置单元格imageView即可。

   [cell.imgview setImage:[UIImage imageNamed:@"placeholderimage.png"]];
   dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_async(concurrentQueue, ^{
            NSError *nserror = nil;
            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString: [imagesArray2 objectAtIndex:indexPath.row]]options:NSDataReadingUncached error:&nserror];
            dispatch_async(dispatch_get_main_queue(), ^{
                if (nserror) {
                    [cell.imgview setImage:[UIImage imageNamed:@"placeholderimage.png"]];
                } else {
                    [cell.imgview setImage:[UIImage imageWithData:imageData]];
                }
            });
        });

Save the url in the cell that will ultimately show the image. 将网址保存在最终显示图片的单元格中。 Then when you attempt to show the image after it arrives make sure the url in the cell matches the url of the image you are trying to show. 然后,当您尝试在到达图像后显示图像时,请确保该单元格中的网址与您要显示的图像的网址匹配。 If it doesn't match don't show it; 如果不匹配,则不显示; this means the cell has been reused (due to scrolling or refresh) and no longer requires that image. 这意味着该单元已被重用(由于滚动或刷新),并且不再需要该图像。 No need to stop the loading of the image, just the display. 无需停止图像加载,只需停止显示即可。

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

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