如何向图像加载添加活动指示器

时间:2016-07-26 04:50:54

标签: ios objective-c iphone uiimageview uiactivityindicatorview

我想在图像视图上添加活动指示器以加载图像。怎么可能请帮助,谢谢

我的代码

-(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];


}

3 个答案:

答案 0 :(得分:0)

请执行以下操作:

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

答案 1 :(得分:0)

使用此代码

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

从块的succes方法中的superview中删除指标。

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

                   }];

}

答案 2 :(得分: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];
    }
相关问题