ios - 在 iOS sdk 中下载图片的多任务处理

标签 ios uitableview

我有一个分别列出 socket 数量的 UItableView,每个 socket 都有一个 Logo 图像。我将这些图像本地保存在 iPhone 上,因此如果在 iPhone 上找到任何图像,它将从那里获取,否则它将发送服务调用并获取数据。它正在相应地工作,但是当我在 tableview 上向下滚动时,它在下载图像时的某个点被挂起,因为它被下载它再次工作正常。有没有什么解决方案可以让我执行下载图像的多任务处理以及通过服务调用或在本地获取图像来表示它们。

这是我的代码..

//本地下载网点图片

    NSLog(@"Downloading...");
    NSString *imageLink = [NSString stringWithFormat:@"http://cf.abc.pk/outlets/l/%@",outs.logo];
    UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageLink]]];

    NSLog(@"%f,%f",image.size.width,image.size.height);
    NSString *docDir =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSLog(@"%@",docDir);
    NSLog(@"saving png");
    NSString *pngFilePath = [NSString stringWithFormat:@"%@/%d.png",docDir,[[arrOutletIds objectAtIndex:webserviceCall] intValue]];

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:pngFilePath];
    if(!fileExists)
    {
        NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
        [data1 writeToFile:pngFilePath atomically:YES];
    }

最佳答案

让我们试试:

//When you download something, execute in background thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
               ^{
                   //download some thing

                dispatch_async( dispatch_get_main_queue(), ^{
                  // when download finish execute in main thread to update data
                  // this function should be called in response of downloading
                }); 
               });

希望对你有帮助。

关于ios - 在 iOS sdk 中下载图片的多任务处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23191775/

相关文章:

ios - 快速动态的 UITableView 行高

ios - 为什么我不能在 viewDidLoad 方法中的静态 UITableViewCell 中设置附件 View ?

ios - 应用程序被 Appstore 拒绝 - 带有问题 PLA 2.3 的 iOS

Swift - 未在表格 View 单元格中触发按钮操作

iphone - 获取所有已安装应用程序的图标列表

ios - 无法为 UITableView 创建导出

ios - 如何在 ios 中舍入表格 View 单元格

ios - 点击 UITabBarItem 像 Instagram 和 Twitter 一样滚动到顶部

ios - 使用node appium-xcuitest-driver测试ios应用程序

ios - 使 NSTimer 失效并创建