2013-08-20 42 views
0

我有一個名爲「ComLog」的「UIImage」返回類型方法。我想從這個方法返回一個圖像。在「ComLog」方法中,我使用GCD從數組中獲取圖像值。我使用下面的代碼,「NSLog(@」qqqqqqqqqqq%@「,exiIco)」打印'image'的值,但NSLog(@「qqqqqqqqqqqq%@」,exiIco);「不要 這裏是細節:如何從GCD返回UIImage類型

-(UIImage*) ComLog 
{ 
ExibitorInfo *currentExibitor100 = [[ExibitorInfo alloc] init]; 
currentExibitor100 = [self.exibitorsArray objectAtIndex:0]; 

imageQueueCompanyLogo = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
dispatch_async(imageQueueCompanyLogo,^
{ 
    UIImage *imageCompanyLogo = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentExibitor100 companyLogoURL]]]]; 
    dispatch_async(dispatch_get_main_queue(),^
    { 
     self.exibitorIcoImageView.image = imageCompanyLogo; 
     exiIco = imageCompanyLogo; 
     NSLog(@"qqqqqqqqqqq %@", exiIco); 
    }); 
}); 
return exiIco; 
} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UIImage *a = [self ComLog]; 
    NSLog(@"It should be a image %@", a); 
} 

這裏所有屬性全局聲明(在「Myclass.h」文件)。我在目的C.新,如果你知道答案,請給予答覆。 在此先感謝。

+0

你的方法返回之前。看看[這個關於GCD的問題](http://stackoverflow.com/q/18178278/1489997),也許它有幫助。 – Sebastian

+0

我不認爲這個問題會有幫助,該方法將在隊列完成之前返回,添加'__block'不會改變它。當隊列完成時,您需要使用回調/塊來將圖像類型返回給調用方法。這裏有一些信息可以幫助你,http://cocoasamurai.blogspot.com/2011/02/practical-design-patterns-with-blocks.html – sbarow

+0

好吧,我會看看這些話題。順便感謝評論。 :) – Tulon

回答

0

第一總之,我建議您閱讀關於Objective C中的塊。您在函數內部使用的dispatch_async塊是異步的,因此在使用它之後會立即返回,因爲它在它自己的池中運行。調用另一種方法t o返回塊內的圖像處理,或在圖像準備就緒後張貼NSNotification。像這樣:

-(void) ComLog 
{ 
    ExibitorInfo *currentExibitor100 = [[ExibitorInfo alloc] init]; 
    currentExibitor100 = [self.exibitorsArray objectAtIndex:0]; 

    imageQueueCompanyLogo = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
    dispatch_async(imageQueueCompanyLogo,^
    { 
     UIImage *imageCompanyLogo = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentExibitor100 companyLogoURL]]]]; 
     dispatch_async(dispatch_get_main_queue(),^
         { 
          self.exibitorIcoImageView.image = imageCompanyLogo; 
          exiIco = imageCompanyLogo; 
          NSLog(@"qqqqqqqqqqq %@", exiIco); 
          [self imageIsReady:exiIco]; 
         }); 
    }); 
// return exiIco; 
} 

- (void)imageIsReady:(uiimage *)image 
{ 
    //do whatever you want with the image 
    NSLog(@"Image is here %@", image); 
} 
+0

這就是所謂的完成處理程序 – Bala

+0

BOOM !! ....是的,它可以像你在這裏做的那樣完成。非常感謝您的回覆。 @Fahri Azimov :) – Tulon

+0

請原諒我,所謂的完成處理程序? –

2

你的代碼片段有太多錯誤,很難決定從哪裏開始。

我建議現在就離開GCD,稍後當你更有經驗時再看看它。

基本上,您想要從遠程服務器加載圖像。 NSURLConnection爲此提供了一個方便的方法這足以滿足非常簡單的用例:

+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler;

您可以在這裏找到的文檔:NSURLConnection Class Reference

加載遠程資源的推薦方法是使用NSURLConnection以異步模式實現代理。你可以在這裏找到更多的信息: URL Loading System Programming Guide - Using NSURL Connection

我也建議閱讀Conventions

下面是一個簡短示例如何使用sendAsynchronousRequest:執行塊

NSURL* url = [NSURL URLWithString:[currentExibitor100 companyLogoURL]]; 
NSMutableURLRequest* urlRequest = [NSURLRequest requestWithURL:url];  
NSOperationQueue* queue = [[NSOperationQueue alloc] init]; 

[NSURLConnection sendAsynchronousRequest:urlRequest 
            queue:queue  
         completionHandler:^(NSURLResponse* response, 
                NSData* data, 
               NSError* error) 
{ 
    if (data) {   
     // check status code, and optionally MIME type 
     if ([(NSHTTPURLResponse*)(response) statusCode] == 200 /* OK */) { 
      UIImage* image = [UIImage imageWithData:data]; 
      if (image) { 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        self.exibitorIcoImageView.image = image; 
       }); 
      } else { 
       NSError* err = [NSError errorWithDomain: ...]; 
       [self handleError:err]; // execute on main thread! 
      }     
     } 
     else { 
      // status code indicates error, or didn't receive type of data requested 
      NSError* err = [NSError errorWithDomain:...]; 
      [self handleError:err]; // execute on main thread! 
     }      
    } 
    else { 
     // request failed - error contains info about the failure 
     [self handleError:error]; // execute on main thread! 
    }   
}]; 
+0

哇!你在這裏發佈的非常足智多謀的帖子。是的,我絕對會在這裏閱讀你的所有建議。非常感謝您的巨大努力。 @CouchDeveloper :) – Tulon