效果圖:
代碼:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)]; imageView.backgroundColor=[UIColor redColor]; [self.view addSubview:imageView]; [self setImageUrlWithActivityIndicator:@"我愛你!" :imageView]; } //網絡請求圖片時,添加loading控件 -(void)setImageUrlWithActivityIndicator:(NSString*)urlString :(UIImageView *)image { if (![urlString isEqualToString:@""]) { // __block UIActivityIndicatorView *activityIndicator = nil; UIActivityIndicatorView *activityIndicator = nil; activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; [image addSubview:activityIndicator]; CGPoint point = CGPointMake(image.frame.size.width/2, image.frame.size.height/2); activityIndicator.center = point; [activityIndicator startAnimating]; [image setImageWithURL:[NSURL URLWithString:[Image_URL stringByAppendingFormat:@"%@",urlString]] placeholderImage:nil success:^(UIImage *image) { NSLog(@"...success code here..."); [activityIndicator stopAnimating]; [activityIndicator removeFromSuperview]; } failure:^(NSError *error) { NSLog(@"... failure code here ..."); [activityIndicator stopAnimating]; [activityIndicator removeFromSuperview]; }]; } }