2013-12-18 25 views
0

我有一個多重Imageview與標籤1,2,3,4 ...將圖像添加到UIImageView及其標籤

現在我想加載該圖像上的圖像與他們各自的標籤。

對於例如:如果6個Imageviews在那裏的話,

for(int i=0;i<6;i++) 
{ 
    imgView = [[UIImageView alloc] initWithFrame:CGRectMake(xpos+10,20, 75, 45)]; 
    xpos+=100; 
    imgView.tag=i; 

    if ([[msg_array objectAtIndex:i] valueForKey:@"Image"]) //Image is cached then assign it 
    { 
     imgView.image=[[msg_array objectAtIndex:i] valueForKey:@"Image"]; 
    } 
    else //Image is not there download it.. 
    { 
     if ([[msg_array objectAtIndex:i] valueForKey:@"Image"] length]!=0) 
     { 
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ 
       [self downloadImage_3:[[msg_array objectAtIndex:i] valueForKey:@"Merchant_SmallImage"] AtIndex:i]; 
      }); 
     } 

    } 

} 


//Downloads Images asynchronously.. 
-(void)downloadImage_3:(NSString*)ImageUrl AtIndex:(int)i{ 

    if ([ImageUrl length]!=0) 
    { 
     UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:ImageUrl]]]; 
     NSLog(@"img = %@",img); 
     imgView.image=img; 
    } 

} 

寫這之後,圖像越來越下載(日誌顯示的img一些編碼值),但它沒有得到應用在其各自的ImageView。

請幫助,並提前致謝。

+0

是你添加你的imageview作爲視圖的子視圖? –

+0

使用圖像延遲加載從url加載圖像 – NANNAV

回答

1

嘗試以下

//change your download method like 
-(void)downloadImage_3:(NSString*)ImageUrl forImageView :(UIImageView *)imgView 
{ 
    if ([ImageUrl length]!=0) 
    { 
     UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:ImageUrl]]]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      [imgView setImage:img]; 
     }); 
    } 
} 

//call your download method like below 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ 
      [self downloadImage_3:[[msg_array objectAtIndex:i] valueForKey:@"Merchant_SmallImage"] forImageView:imgView]; //pass your ImageView here 
     }); 
+0

我寫了' - (void)downloadImage_3:(NSString *)ImageUrl forImageView:(UIImageView *)imgVw { NSLog(@「ImageUrl =%@」,ImageUrl); if([ImageUrl length]!= 0) UIImage * img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:Path]]]; if(img){dicImages_msg setObject:img forKey:Path]; } imgView.image = [dicImages_msg valueForKey:Path]; } }'ImageUrl顯示Url的路徑,但仍然沒有應用圖像 – Krunal

+0

什麼是'Path'?你使用了'[Path length]'。 –

+0

抱歉'路徑'表示'ImageUrl'檢查編輯 – Krunal

0

如果您要添加的分配圖像視圖到父視圖,它應該在裏面被做了僅環,每次到一個指定圖像的圖像 - 查看您需要將此圖像視圖添加到父視圖以顯示它,請檢查。

+0

如果我這樣做,下載圖像需要很多時間,我不想在主線程上執行下載。 – Krunal