你好,歡迎來到IOS教程網

 Ios教程網 >> IOS使用技巧 >> IOS技巧綜合 >> 網絡請求圖片

網絡請求圖片

編輯:IOS技巧綜合
[摘要]本文是對網絡請求圖片的講解,對學習IOS蘋果軟件開發有所幫助,與大家分享。

效果圖:

代碼:

- (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];
         }];
    }
}
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved