你的位置:首页 > 操作系统

[操作系统]网络请求图片


 

效果图:

 

代码:

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