简体   繁体   English

使用活动指示器在viewDidLoad中异步加载图像

[英]loading image asynchronously in viewDidLoad, with activity indicator

When i'm switching from a view (by clicking on a cell in a table) to a view that displays some images, i'm loading a couple of images from a couple of urls. 当我从视图(通过单击表中的单元格)切换到显示一些图像的视图时,我正在从几个URL中加载几个图像。

I want to display an activity indicator animation while the loading occurs. 我想在加载发生时显示活动指示器动画。

I'm doing this loading in viewDidLoad of the new view. 我正在新视图的viewDidLoad中进行此加载。

If i'm loading the images synchronously, then (not surprisingly) the animation doesn't work since the request is blocking... If i'm loading the images asynchronously, then (also not surprisingly) the view is opened with blanks instead of images, without waiting for the images to be fetched, which i don't want. 如果我正在同步加载图像,那么(不足为奇)该动画将不起作用,因为请求被阻止了...如果我是异步加载图像,则(也不足为奇)该视图用空白打开了图像,而无需等待图像被提取,这是我不想要的。

I tried to put all that in the segue code that transforms from the old view to the new one, because i hoped that the view will be switched only after the loading will complete but it didn't matter. 我试图将所有这些内容都放入从旧视图转换到新视图的segue代码中,因为我希望仅在加载完成后才切换视图,但这没关系。

How can I enjoy both worlds? 我怎样才能享受两个世界? how can i not block the app, display the animation, but transition to the next view only when all the images have been loaded? 我如何才能不阻止该应用程序,显示动画,而是仅在所有图像均已加载后才过渡到下一个视图?

If you don't want to use third party libraries, you can use GCD (Grand central dispatch) like this: 如果您不想使用第三方库,则可以使用GCD(大型中央调度),如下所示:

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
    });
});

Have a look at AFNetworking and their UIImageView Extension : 看看AFNetworking及其UIImageView扩展

setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholderImage;

Asynchronously downloads an image from the specified URL, and sets it once the request is finished. 从指定的URL异步下载图像,并在请求完成后进行设置。
If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished. 如果图像在本地缓存,则立即设置图像,否则将立即设置指定的占位符图像,然后在请求完成后设置远程图像。

You should take a look at AsyncImageView . 您应该看看AsyncImageView It does exactly what you want to do. 它正是您想要做的。

You can find it at: https://github.com/nicklockwood/AsyncImageView 您可以在以下位置找到它: https : //github.com/nicklockwood/AsyncImageView

If you don't want to show certain views while you loading images you can simply hide it, like this self.imageView.hidden = YES; 如果您不想在加载图像时显示某些视图,则可以将其隐藏起来,例如self.imageView.hidden = YES; After loading you can set your images, and then set hidden to NO . 加载后,您可以设置图像,然后将hidden设置为NO

If you, for some reason, don't want to segue to another view, before loading completes, then I think you can make intermediate view, where you'll download all images, after that segue to your images view, and send that images through prepareForSegue:sender: . 如果由于某种原因您不想在加载完成之前选择其他视图,那么我认为您可以创建中间视图,在此视图中将所有图像下载到图像视图中,然后将其发送到该视图通过prepareForSegue:sender:

And answer from Aurelien Cobb cleared out how to load images asynchronously. 来自Aurelien Cobb的回答阐明了如何异步加载图像。

Check SDWebImage ( https://github.com/rs/SDWebImage ), it has something called prefetcher - which will download the images and save them in cache, after which you will receive a callback, and can transition to the new view. 检查SDWebImage( https://github.com/rs/SDWebImage ),它有一个称为prefetcher的东西-将下载图像并将其保存在缓存中,此后您将收到回调,并可以转换到新视图。 Needless to say that you will have to show a spinner (SVProgressHUD or MBProgressHUD), and stop it once prefetch callback returns. 不用说,您将必须显示一个微调器(SVProgressHUD或MBProgressHUD),并在预取回调返回后停止它。

https://github.com/rs/SDWebImage/blob/master/SDWebImage/SDWebImagePrefetcher.h https://github.com/rs/SDWebImage/blob/master/SDWebImage/SDWebImagePrefetcher.h

/**
 * Assign list of URLs to let SDWebImagePrefetcher to queue the prefetching,
 * currently one image is downloaded at a time,
 * and skips images for failed downloads and proceed to the next image in the list
 *
 * @param urls list of URLs to prefetch
 */
- (void)prefetchURLs:(NSArray *)urls;

To show those images in your imageView, just use setImageWithURL: method (UIImageView+WebCache category). 要在imageView中显示这些图像,只需使用setImageWithURL:方法(UIImageView + WebCache类别)。

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

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