xcode 为 URL 加载的 UIImage 的 UIActivityIndi​​catorView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8531676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:45:23  来源:igfitidea点击:

UIActivityIndicatorView for an UIImage that load for an URL

iphonexcodeurluiimageuiactivityindicatorview

提问by Alberto Juarez

I have an UIImagethat is loaded from a URL, and i want to show an UIActivityIndicatorViewanimating while the image loads. Someone can help me?

我有一个UIImage从 URL 加载的,我想UIActivityIndicatorView在加载图像时显示动画。有人可以帮助我吗?

Thanks for all.

谢谢大家。

回答by Sri

So u want to use the UIActivityIndicatorwhile accessing the server.

所以你想UIActivityIndicator在访问服务器时使用。

First create the indicator while the connection call, and when connection didfinishloading remove the activity indicator

首先在连接调用时创建指示器,并在连接didfinishloading时移除活动指示器

//call this when connection start
UIActivityIndicator *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(140, 236, 37, 37);
[activityIndicator startAnimating];
[self.view addSubview:activityIndicator];
self.view.userInteractionEnabled = NO;

//remove activity indicator while connection did finish loadin
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [activityIndicator stopAnimating];
    [activityIndicator removeFromSuperview];
    self.view.userInteractionEnabled = YES;
}

回答by Hardik Shah

First of all add Activity indicator in xib and connect object of the UIActivity Indicator.

首先在xib中添加Activity指标并连接UIActivity指标的对象。

Click "Hides when Stopped" in the xib.

单击 xib 中的“停止时隐藏”。

Then,

然后,

-(void)FetchFromServer

{

NSURLRequest* updateRequest = [NSURLRequest requestWithURL: [NSURL URLWithString:@"Write Your Url Here"]];

NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:updateRequest  delegate:self];

[connection start];

[activityIndicator startAnimating];

}

And then in the delegate,

然后在代表中,

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

UIImage *img = [UIImage imageWithData: data];

myImageView.image=img;

[activityIndicator stopAnimating];

}