2013-04-12 174 views
0

我使用給定代碼從各自的網址下載圖片,但由於某些問題,它無法正常工作,我無法診斷問題。請幫我解決這個問題。 在此先感謝。異步下載圖片

-(void)createScrollViewControls 
{ 
    [self removeAllControlsFromScrollView]; 

    totalNumberOfImages=[arrImages count];  

    __block int x=0; 
    __block int y=0; 
    __block int scroll=0; 
    __block UIImage *tempimag=nil; 

    if(totalNumberOfImages>0) //If product images are there 
    { 
//  imageScroll.contentSize=CGSizeMake(imageScroll.frame.size.width*totalNumberOfImages, IMAGEHEIGHT); 

     for(int i=0;i<totalNumberOfImages;i++){ 

      NSLog(@"Value=%@",arrImages); 

      UIActivityIndicatorView *activity= [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(77+scroll, 38, 30, 30)]; 
      [activity setBackgroundColor:[UIColor clearColor]]; 
      [activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray]; 
      [imageScroll addSubview:activity]; 
      [activity startAnimating];    

      dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
      dispatch_async(concurrentQueue, ^{ 

      NSURL *imageURL=[NSURL URLWithString:[arrImages objectAtIndex:i]]; 

       NSData *tempData=[NSData dataWithContentsOfURL:imageURL]; 
       UIImage *tImage=[UIImage imageWithData:tempData]; 

       if (tImage==nil) 
       { 
        tImage=[UIImage imageNamed:@"NoImage.png"]; 
       } 
       dispatch_async(dispatch_get_main_queue(), ^{ 


        tempimag= tImage; 

        UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(x, y, IMAGEWIDTH, IMAGEHEIGHT)]; 

      //if url is there but image not getting downloaded then show a static image 
        if (!tempimag) 
        { 
         imageScroll.contentSize=CGSizeMake(imageView.frame.size.width, IMAGEHEIGHT); 

         NSLog(@"no underlying data"); 
         imageView.image = [UIImage imageNamed:kNoImageAvailable]; 
         [imageScroll addSubview:imageView]; 
         // totalNumberOfImages = 0; 
        } 
        else{ 

         [activity stopAnimating]; 
         imageScroll.contentSize=CGSizeMake(imageScroll.frame.size.width*totalNumberOfImages, IMAGEHEIGHT); 
         imageView.image=tempimag; 
         [imageScroll addSubview:imageView]; 
         x=x+imageScroll.frame.size.width; 

        }  

       }); 

      }); 

/* 
      UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(x, y, IMAGEWIDTH, IMAGEHEIGHT)]; 



    if (!tempimag)//if url is there but image not getting downloaded then show a static image 
      { 
       imageScroll.contentSize=CGSizeMake(imageView.frame.size.width, IMAGEHEIGHT); 

       NSLog(@"no underlying data"); 
       imageView.image = [UIImage imageNamed:kNoImageAvailable]; 
       [imageScroll addSubview:imageView]; 
       totalNumberOfImages = 0; 
       break; 
      } 
      else{ 
       imageScroll.contentSize=CGSizeMake(imageScroll.frame.size.width*totalNumberOfImages, IMAGEHEIGHT); 
       imageView.image=tempimag; 
       [imageScroll addSubview:imageView]; 
       x=x+imageScroll.frame.size.width; 
      } 
     } */ 

      scroll+=184; 
    } 
} 
+0

和你的問題是什麼? –

+0

URL中有白色空格,因此正在創建問題其他方面的代碼正在運行fine.i發現問題,我解決了它。感謝回覆。 – garry007

回答

0

這低於行代碼:

if (tImage==nil) 
      { 
       tImage=[UIImage imageNamed:@"NoImage.png"]; 
      } 

總會讓你的程序的控制轉到:

if (!tempimag) 
       { 
        imageScroll.contentSize=CGSizeMake(imageView.frame.size.width, IMAGEHEIGHT); 

        NSLog(@"no underlying data"); 
        imageView.image = [UIImage imageNamed:kNoImageAvailable]; 
        [imageScroll addSubview:imageView]; 
        // totalNumberOfImages = 0; 
       } 

請刪除提到的代碼的第一部分,並嘗試一下。