2012-08-15 34 views
0

NSArray圖像有:NSArray的圖片不顯示在滾動型

2012-08-15 10:12:39.687 test[4200:11103] (
    "http://192.168.1.165/Images/Demo/1.jpg", 
    "http://192.168.1.165/Images/Demo/0.jpg", 
    "http://192.168.1.165/Images/Demo/0.jpg", 
    "http://192.168.1.165/Images/Demo/2.jpg", 
    "http://192.168.1.165/Images/Demo/2.jpg", 
    "http://192.168.1.165/Images/Demo/2.jpg", 
    "http://192.168.1.165/Images/Demo/2.jpg", 
    "http://192.168.1.165/Images/Demo/1.jpg", 
    "http://192.168.1.165/Images/Demo/1.jpg", 
    "http://192.168.1.165/Images/Demo/1.jpg") 

和代碼的.m:

dispatch_async(htvque, ^{ 
     NSData* data = [NSData dataWithContentsOfURL: tophotfilm]; 

     NSError* error; 

     json = [NSJSONSerialization JSONObjectWithData:data 
               options:kNilOptions 
               error:&error]; 

     NSDictionary *list = [json objectForKey:@"List"]; 


     NSMutableArray *arrPoster =[[NSMutableArray alloc]init]; 
     for(NSString *po in list) 
     { 
      NSString *poster = [po valueForKey:@"Poster"]; 
      [arrPoster addObject:poster]; 

     } 
     myArray = [NSArray arrayWithArray:arrPoster]; 
     NSArray *colors = [[NSArray alloc] initWithObjects:[UIColor redColor], 
          [UIColor greenColor], [UIColor magentaColor], 
          [UIColor blueColor], [UIColor orangeColor], 
          [UIColor brownColor], [UIColor grayColor], nil]; 
      //NSLog(@"%@",colors); 
      NSLog(@"%@",myArray); 
     for (int i = 0; i < myArray.count; i++) { 
       //NSLog(@"%@",myArray.count); 
      NSString *imageName = [NSString stringWithFormat:@"%@", i]; 
      UIImage *image = [UIImage imageNamed:imageName]; 
      UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

      CGRect rect = imageView.frame; 

      rect.origin.x = self.scrollView.frame.size.width * i; 
      rect.origin.y = 0; 
      rect.size = scrollView.frame.size; 

      [self.scrollView addSubview:imageView]; 

     } 

     scrollView.pagingEnabled = YES; 
     scrollView.showsHorizontalScrollIndicator = NO; 
     scrollView.showsVerticalScrollIndicator = NO; 
     scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 
     myArray.count,scrollView.frame.size.height); 
     pageControl.currentPage = 0; 
     pageControl.numberOfPages = myArray.count; 
     [super viewDidLoad]; 
    }); 

隨着NSArray的顏色,它的工作好,但NSArray的形象有一定的誤差。


滾動視圖不顯示的ListImage,它顯示出第一圖像,有人知道爲什麼...

dispatch_async(dispatch_get_main_queue(),^ { 對(INT I = 0;我< myArray的.Count之間;我++){ // * NSString的imageName = [NSString的stringWithFormat:@ 「%@」,[myArray的objectAtIndex:ⅰ]];

   NSURL *urlImage = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[myArray objectAtIndex:i]]]; 
       //NSLog(@"%@",urlImage); 
       UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:urlImage]]; 
       UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

       CGRect rect = imageView.frame; 
       rect.size.height = imgHeight; 
       rect.size.width = imgWidth; 
       imageView.frame = rect; 
       imageView.tag = i; 

       //rect.size = scrollView.frame.size; 
       [scrollView addSubview:imageView]; 
      } 

      scrollView.pagingEnabled = YES; 
      scrollView.showsHorizontalScrollIndicator = NO; 
      scrollView.showsVerticalScrollIndicator = NO; 
      scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 

myArray.co UNT,scrollView.frame.size.height); pageControl.currentPage = 0; pageControl.numberOfPages = myArray.count; });

回答

0

檢查這行代碼

NSString *imageName = [NSString stringWithFormat:@"%@", i];

你形象的名字也不過1,2等,沒有在日誌中打印的圖像的名字,

它應該是這樣的

[myArray objectATIndex:i];

+0

喜ashkbabuy,我固定的網址,並獲得圖像,但它們不會出現在的UIImage也許它分爲「dispatch_async(htvque,^ {} ..//」 – TrungVu 2012-08-15 05:51:42

0

您正在使用imageNamed:,它只能用於你的項目中的圖像,如果我理解你的問題,你試圖從服務器訪問它們。試試這個方法UIImageimageWithData:

NSURL *url = [NSURL URLWithString:[myArray objectAtIndex:i]]; 
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]]; 
+0

感謝OHR ,我使用的代碼爲ü,它的運行正常,但它不顯示列表圖像在scrollView我不知道爲什麼---這個鏈接代碼: http://mocku.ps/hwcqot – TrungVu 2012-08-17 03:46:05