2011-05-19 84 views
0

我有兩個視圖控制器。在第一個UIViewController中,我有一個按鈕,點擊後會將您帶到顯示兩個圖像的第二個視圖控制器。圖像來自URL,因此我使用NSData並初始化一個UIImage,並將其分配給UIImageView。UIButton凍結

現在的問題是,當我點擊第一個視圖控制器中的UIButton按鈕保持按下狀態幾秒鐘,然後它移動到第二個視圖控制器來顯示圖像。

UIImage *Image1=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:URL_STRING]]]; 
    imageView1.contentMode=UIViewContentModeScaleAspectFit; 
    imageView1.image=Image1; 
    UIImage *Image2=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:URL_STRING]]]; 
    imageView2.contentMode=UIViewContentModeScaleAspectFit; 
    imageView2.image=agent_logo_image; 

請問任何人建議我解決這個問題的方法。

謝謝

+0

什麼這裏的agent_logo_image?/ – 2011-05-19 09:27:38

+0

在哪個控制器下載此圖片,第一或第二調用此函數? – itZme 2011-05-19 09:28:33

回答

0

您的問題是,您正在進行同步請求,以獲取圖像。這意味着您的主線程正在等待圖像下載,然後繼續處理其餘的流程。所以,「凍結」按鈕只是表示主線程正在發生什麼事情,而這個「事情」需要先完成。 解決方法是進行異步調用以獲取圖像。我建議使用ASIHTTPRequest這類東西,因爲它比較容易使用,並且可以讓你免去很多工作。如果您不想使用庫,則需要查看NSURLConnection類參考中的「Loading Data Asynchronously」。

0

基本上觸摸按鈕..你連接到URL來獲取圖像。

主線程上的此過程會將UI掛起幾秒鐘。

我建議你把圖像抓取部分:

self performSelectorInBackground:@selector() withObject:功能。

然後掠烏爾圖像使用後:

self performSelectorOnMainThread:@selector() withObject: waitUntilDone: 切換回烏爾主線程並顯示烏爾圖像。

希望它有助於.. :)

0

根據您的問題將圖像數據從互聯網未來的權利,因此需要一定的時間,使數據從互聯網上拉,被顯示在ImageView的這樣在這種情況下,我會建議你是使用一個線程這樣的事情繼承人,我已根據您的問題,使用的代碼應該工作完美

 
-(void)getImages 
{ 
    NSString *firstImageURL = @"http://fc00.deviantart.net/fs33/i/2008/293/8/6/Wood_Apple_Wallpaper_by_diegocadorin.jpg"; 
    NSString *secondImageURL = @"http://i1-mac.softpedia-static.com/screenshots/Byte-Apple-Wallpaper_3.png"; 
    NSAutoreleasePool *threadPool = [[NSAutoreleasePool alloc]init]; 

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:firstImageURL]]; 
    NSData *data2 = [NSData dataWithContentsOfURL:[NSURL URLWithString:secondImageURL]]; 
    UIImage * img = [[UIImage alloc]initWithData:data]; 
    UIImage *_img = [[UIImage alloc]initWithData:data2]; 
    img1.image = img ; 
    img2.image = _img; 
    [img release]; 
    [_img release]; 
    [threadPool drain]; 

} 

這裏IMG1和IMG2是的UIImageView的對象

和到init方法使使用NSThread類

 
[NSThread detachNewThreadSelector:@selector(getImages) toTarget:self withObject:nil]; 

感謝和問候