2016-07-26 147 views
0

我想将活动指示器添加到图像加载图像视图上。它是如何可能的话请帮忙,谢谢如何将活动指示器添加到图像加载

我的代码

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
NSError *error; 

NSLog(@"Error in receiving data %@",error); 
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; 
NSLog(@"response data %@",json); 

NSArray* results = [json objectForKey:@"status"]; 
NSArray *imageUrlArray = [results valueForKey:@"slider_image_path"]; 
NSLog(@"images %@",imageUrlArray); 

NSMutableArray *arrayImages = [[NSMutableArray alloc] init]; 

for (NSString *strImageUrl in imageUrlArray) { 
    [arrayImages addObject:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strImageUrl]]]]; 
} 


self.imageview.animationImages = arrayImages; 
_imageview.animationDuration = 10; 
_imageview.animationRepeatCount = 0; 
[_imageview startAnimating]; 


} 
+0

似乎代表connectionDidFinishLoading应停止指标。开始 - 在另一个代表。或者以其他方式:启动 - 在异步数据读取任务之前,停止 - 关闭此任务。 – drewpts

+0

@drewpts如何可能请解释 –

+0

@ sm5345已经解释了这个想法。主要想法是在异步请求中停止指示符。因此,你不知道什么时候会完成异步任务 - 你需要在异步任务之后放置停止命令,但是完全在其中:在objc块中查看'''[removeFromSuperview]'''顺便说一下,如果你还不知道这些东西,请阅读objc中的块或swift中的闭包以及它们在异步任务中的角色。和平。 – drewpts

回答

0

不喜欢以下内容:

UIActivityIndicatorView *indctr = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
[indctr startAnimating]; 
[indctr setCenter:self.imageView.center]; 
[self.contentView addSubview:indctr]; 
+0

这种方法只添加用户界面,但我想要一个图像加载开始指标后加载停止指标 –

0

使用此代码

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
[indicator startAnimating]; 
[indicator setCenter:self.imageView.center]; 
[self.contentView addSubview:indicator]; 

从SuperView把取下指示器块的成功方法。

[_imageView setImageWithURL:[NSURL URLWithString:anURL] 
        success:^(UIImage *image) { 
         [indicator removeFromSuperview]; 
        } 
        failure:^(NSError *error) { 

        }]; 

}

+0

如何使用此方法在我的代码 –

+0

for(NSString * strImageUrl in imageUrlArray){UIImageView * imageView = [[UIImage alloc] init]; UIActivityIndi​​catorView * indicator = [[UIActivityIndi​​catorView alloc] initWithActivityIndi​​catorStyle:UIActivityIndi​​catorViewStyleGray]; [indicator startAnimating]; [indicator setCenter:self.imageView.center]; [self.contentView addSubview:indicator]; [imageView setImageWithURL:[NSURL:strImageUrl] 成功:^(UIImage *图像){ [indicator removeFromSuperview]; }失败:^(NSError * error){ }]; [arrayImages addObject:imageView]; } – SM18

+0

替换为此循环 – SM18

0

你可以使用MBProgress HUD类:https://github.com/jdg/MBProgressHUD 下载它,并在代码中设置这样的:

-(void)viewDidLoad{ 
MBProgressHUD *HUD = [[MBProgressHUD alloc]initWithView:self.view]; 
     [self.view addSubview:HUD]; 
} 
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection 
    { 
    NSError *error; 

    NSLog(@"Error in receiving data %@",error); 
    [HUD show: YES]; 
    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; 
    NSLog(@"response data %@",json); 

    NSArray* results = [json objectForKey:@"status"]; 
    NSArray *imageUrlArray = [results valueForKey:@"slider_image_path"]; 
    NSLog(@"images %@",imageUrlArray); 

    NSMutableArray *arrayImages = [[NSMutableArray alloc] init]; 

    for (NSString *strImageUrl in imageUrlArray) { 
     [arrayImages addObject:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strImageUrl]]]]; 
    } 


    self.imageview.animationImages = arrayImages; 
    _imageview.animationDuration = 10; 
    _imageview.animationRepeatCount = 0; 
    [_imageview startAnimating]; 

    [HUD hide: YES]; 
    } 
+0

导入MB进度HUD但HUD未声明标识 –

+0

@AnkurKumawat检查我更新的答案。 –

相关问题