2009-12-29 45 views
0

我寫了我的代碼-(void)loadView{ }使用NSURL從互聯網上獲取圖像。但在加載圖像之前,我需要顯示微調器(UIActivityIndicatorView)。在哪裏可以使用UIActivityIndi​​catorView?

#import "ImageFromWebViewController.h" 
#define USE_TEST_SERVER 1 
@implementation ImageFromWebViewController 
+(NSString *)fileName 
{ 
#if USE_TEST_SERVER 
    return @"http://happyhyderabad.files.wordpress.com/2009/04/anushka4.jpg"; 
#else 
    return @"http://nutritionresearchcenter.org/healthnews/wp-content/uploads/2008/07/johnny_depp.jpg"; 
#endif 
} 

- (void)loadView { 
    NSString *urlString = [ImageFromWebViewController fileName]; 
    NSURL *url = [NSURL URLWithString:urlString]; 
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]]; 
    imageView = [[UIImageView alloc] initWithImage:image]; 
    contentView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    [contentView setContentSize:[image size]]; 
    [contentView addSubview:imageView]; 
    [imageView setUserInteractionEnabled:NO]; 
    self.view = contentView; 
} 

- (void)didReceiveMemoryWarning { 
     [super didReceiveMemoryWarning]; 
     } 

- (void)dealloc { 
    [imageView release]; 
    [contentView release]; 
    [super dealloc]; 
} 
@end 

viewDidLoad我寫的代碼爲UIActivityIndicatorView,但圖像的加載後的微調開始,並沒有停止。

我應該在哪裏編寫微調代碼?

回答

1

您需要先在後臺線程下載圖像。在viewDidLoad中,您需要啓動您的微調器,然後啓動後臺線程。隱藏微調和繪製圖像取決於你如何去做。下載完成後,您可以在後臺線程中隱藏微調器,但嚴格來說,通常最好不要修改除主線程之外的任何線程的用戶界面。

如果你不想打擾處理你自己的後臺線程,看看[NSURLConnection connectionWithRequest:delegate:]。這將啓動自己的後臺線程,以允許您異步加載數據。在這種情況下,仍然在viewDidLoad中啓動微調器,然後處理NSURLConnection的內容,並在數據完成下載時調用的委託回調方法中隱藏微調器(因爲您將回到主線程中)點,我相信)。

相關問題