2013-09-30 107 views
0

我正在做以下事情從服務器加載我的圖像。但問題是,當alertview關閉時,imageview在5-7秒後顯示。如何從服務器異步獲取圖像

如何解決這個問題?

- (void)showFullImage :(id) sender 
    {  
    // for loading view 
    self.loading_alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"LOADING",nil) message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil]; 
    [self.loading_alert show]; 
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithFrame: CGRectMake(125, 65, 30, 30)]; 
    indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge; 

    //indicator.center = CGPointMake(loading_alert.bounds.size.width/2, loading_alert.bounds.size.height - 50); 
    [indicator startAnimating]; 
    [self.loading_alert addSubview:indicator];  

    self.image_url = [self.question_set valueForKey:@"image_url"]; 

//--------Asyncronously fetch image---------------------------------------- 
    NSURL *URL = [NSURL URLWithString:image_url]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLCacheStorageAllowedInMemoryOnly 
                timeoutInterval:60.0]; 
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){ 

    NSLog(@"1",nil); 
    imgView= [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480) ]; 

    UIImage* queImage = [UIImage imageWithData:data]; 

UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,42,320,420)]; 
iv.image = queImage; 

[imgView addSubview:iv];  
[self.view addSubview:imgView]; 

    //dismiss alert view on main thread 
    dispatch_async(dispatch_get_main_queue(), ^(void) { 
     // dismiss alert view... 
     NSLog(@"2",nil); 
     [self.loading_alert dismissWithClickedButtonIndex:0 animated:YES]; 
    }); 

}];  
} 

幫助將不勝感激。

回答

0

您需要在主隊列中的後臺隊列和UI更新中下載圖像。

使用GCD這樣,

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ 

     NSURL *URL = [NSURL URLWithString:image_url]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLCacheStorageAllowedInMemoryOnly 
                timeoutInterval:60.0]; 
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){ 

    NSLog(@"1",nil); 


    UIImage* queImage = [UIImage imageWithData:data]; 

      dispatch_async(dispatch_get_main_queue(), ^{ 

       imgView= [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480) ]; 
       if([receivedView isKindOfClass:[UIImageView class]]) 
       { 
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,42,320,420)]; 
iv.image = queImage; 

[imgView addSubview:iv];  
[self.view addSubview:imgView]; 
       } 
// dismiss alert view... 
     NSLog(@"2",nil); 
     [self.loading_alert dismissWithClickedButtonIndex:0 animated:YES]; 

      }); 


     } 


    }); 
0

SDWebmage - >這是一個加載圖像一個很好的第三方庫。

0

忘記GCD,您可以在NSURLConnection的的sendAsynchronousRequest的隊列參數傳遞[NSOperationQueue mainQueue]:隊列:completionHandler:方法,所以你不需要考慮太多關於主線程和後臺線程,它會自動處理呼叫在後臺請求並在主線程上返回數據。

及以下代碼將如何看起來像:

NSURL *URL = [NSURL URLWithString:image_url]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLCacheStorageAllowedInMemoryOnly timeoutInterval:60.0]; 

// request is called asynchronously and response completion handler will be called on main thread 
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){ 

    // do your whatever work here as you do normally, because it's in main thread 
    NSLog(@"1",nil); 
    imgView= [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480) ]; 

    UIImage* queImage = [UIImage imageWithData:data]; 

    UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,42,320,420)]; 
    iv.image = queImage; 

    [imgView addSubview:iv]; 
    [self.view addSubview:imgView]; 
}