Activity 下载图片时的指示器 iOS
Activity indicator while downloading images iOS
我正在下载一组图像来创建动画。我怎样才能让下载开始时显示 activity 指示器以及下载完成时隐藏 activity 指示器。
self.img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.width-70)];
img.animationImages = [NSArray arrayWithObjects:
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
img.animationDuration = 20.0f;
img.animationRepeatCount = 0;
[img startAnimating];
[self.view addSubview: img];
您的图片正在同步下载,这意味着您正在阻塞主线程(这很糟糕)。相反,运行 在后台线程上下载,这将允许主线程,并且在扩展中,您的应用程序的 UI 仍然可以运行。使用您的示例代码,这是当 运行 在后台时的样子:
self.img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.width-70)];
self.img.animationDuration = 20.0f;
self.img.animationRepeatCount = 0;
[self.img startAnimating];
[self.view addSubview:self.img];
// START ANIMATING YOUR ACTIVITY INDICATOR HERE
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
NSArray *images = @[
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
];
dispatch_async(dispatch_get_main_queue(), ^(void) {
// STOP ANIMATING YOUR ACTIVITY INDICATOR HERE
[self.img setAnimationImages:images];
});
});
下载开始时编写此代码:
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] ;
spinner.frame = CGRectMake(0, 0, 320, 50);
[微调器启动动画];
下载结束时编写此代码:[spinner stopAnimating];
我正在下载一组图像来创建动画。我怎样才能让下载开始时显示 activity 指示器以及下载完成时隐藏 activity 指示器。
self.img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.width-70)];
img.animationImages = [NSArray arrayWithObjects:
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
img.animationDuration = 20.0f;
img.animationRepeatCount = 0;
[img startAnimating];
[self.view addSubview: img];
您的图片正在同步下载,这意味着您正在阻塞主线程(这很糟糕)。相反,运行 在后台线程上下载,这将允许主线程,并且在扩展中,您的应用程序的 UI 仍然可以运行。使用您的示例代码,这是当 运行 在后台时的样子:
self.img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.width-70)];
self.img.animationDuration = 20.0f;
self.img.animationRepeatCount = 0;
[self.img startAnimating];
[self.view addSubview:self.img];
// START ANIMATING YOUR ACTIVITY INDICATOR HERE
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
NSArray *images = @[
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
];
dispatch_async(dispatch_get_main_queue(), ^(void) {
// STOP ANIMATING YOUR ACTIVITY INDICATOR HERE
[self.img setAnimationImages:images];
});
});
下载开始时编写此代码: UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] ; spinner.frame = CGRectMake(0, 0, 320, 50); [微调器启动动画];
下载结束时编写此代码:[spinner stopAnimating];