当我从 View (通过单击表格中的单元格)切换到显示一些图像的 View 时,我正在从几个网址加载一些图像。
我想在加载时显示事件指示器动画。
我正在新 View 的 viewDidLoad 中进行加载。
如果我同步加载图像,那么(毫不奇怪)动画将不起作用,因为请求被阻止...... 如果我异步加载图像,那么(也不足为奇) View 将用空白而不是图像打开,而不等待图像被获取,这是我不想要的。
我尝试将所有这些都放在从旧 View 转换为新 View 的segue代码中,因为我希望仅在加载完成后才切换 View ,但这并不重要。
如何享受两个世界?如何才能不阻止应用程序、显示动画,而是仅在所有图像加载完毕后才过渡到下一个 View ?
最佳答案
如果你不想使用第三方库,你可以使用GCD(Grandcentraldispatch),如下所示:
dispatch_queue_t imageLoadingQueue = dispatch_queue_create("imageLoadingQueue", NULL);
// start the loading spinner here
dispatch_async(imageLoadingQueue, ^{
NSString * urlString = // your URL string here
NSData * imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
UIImage * image = [UIImage imageWithData:imageData];
dispatch_async(dispatch_get_main_queue(), ^{
// stop the loading spinner here and place the image in your view
});
});
关于ios - 在 viewDidLoad 中异步加载图像,并带有事件指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22743444/