码迷,mamicode.com
首页 > 其他好文 > 详细

网络请求图片

时间:2015-03-12 11:02:07      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

 

效果图:

 

技术分享

代码:

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

 

网络请求图片

标签:

原文地址:http://www.cnblogs.com/yang-guang-girl/p/4331560.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!