2016-09-08 99 views
0

我試圖通過PHP在集合視圖中加載圖像,如下面的代碼所示;集合查看未顯示圖像

@interface SearchMainPost() 
{ 

    NSMutableArray *myObject; 
    // A dictionary object 
    NSDictionary *dict; 
    // Define keys 
    NSString *imageid; 
    NSString *name; 
    NSString *path; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    // Define keys 

    imageid = @"videoImage"; 
    name = @"timeLineVideoUserName"; 
    path = @"TheIndex"; 

    // Create array to hold dictionaries 
    myObject = [[NSMutableArray alloc] init]; 


    NSData *jsonData = [NSData dataWithContentsOfURL: 
         [NSURL URLWithString:@"http://mywebSite/list/list.php"]]; 


    id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]; 

    // values in foreach loop 
    for (NSDictionary *dataDict in jsonObjects) { 

     NSString *strImageID = [dataDict objectForKey:@"videoImage"]; 

     NSString *strName = [dataDict objectForKey:@"timeLineVideoUserName"]; 

     NSString *strPath = [dataDict objectForKey:@"TheIndex"]; 

     dict = [NSDictionary dictionaryWithObjectsAndKeys: 

       strImageID, imageid, 
       strName, name, 
       strPath, path, 
       nil]; 
     [myObject addObject:dict]; 


     NSLog(@"%@", strImageID); 
    } 
} 

她是我如何實現收集;

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { 
    return 1; 
} 


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
     return myObject.count; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

    SearchMainPostCell *myCell = [collectionView 
           dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath]; 

    NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row]; 

    NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:path]]; 
    NSData *data = [NSData dataWithContentsOfURL:url]; 
    UIImage *img = [[UIImage alloc] initWithData:data]; 

    myCell.displayImage.image = img; 

    // myCell.displayDetail.text= [tmpDict objectForKey:name]; 


    return myCell; 

} 

我的問題是圖像沒有顯示在單元格中,所有的集合視圖都是黑色的。 我可以看到控制檯中顯示的信息,我可以看到圖像鏈接。集合視圖單元已被設置並在屬性檢查器中將標識符設置爲「myCell」。

我很抱歉我的解釋,我希望有人會幫助我如何顯示圖像。

在此先感謝。

回答

0

在tableview中請求URL應該是異步的,因爲異步任務必須使用GCD (Grand Central Dispatch) .handle後臺任務和dispatch_async。您也可以從viewDidLoad中獲取並處理來自PHP的JSON數據,它將凍結用於數據下載的UI。這不是下載數據的好方法。也可以使用GCD下載。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

    SearchMainPostCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 

    NSDictionary *dict = [dataArray objectAtIndex:indexPath.row]; 

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

     NSURL *url = [NSURL URLWithString:[dict objectForKey:path]]; 

     NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]; 

     UIImage* image = [[UIImage alloc] initWithData:imageData]; 
     if (image) { 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       cell.imageView.image = image; 
       [cell setNeedsLayout]; 
      }); 
     } 
    }); 

    return cell; 
} 

編輯:

@interface ViewController() 
{ 
    NSCache *imageCache; 
} 

在viewDidLoad中:

imageCache = [[NSCache alloc] init]; 

在的CollectionView:cellForItemAtIndexPath:

if([imageCache objectForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]]) 
{ 
    cell.image.image = [imageCache objectForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]]; 
} 
else 
{ 
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
    dispatch_async(queue, ^(void) { 

     NSURL *url = [NSURL URLWithString:[self.imageArray objectAtIndex:indexPath.row]]; 
     NSData *imageData = [NSData dataWithContentsOfURL:url]; 
     UIImage* image = [[UIImage alloc] initWithData:imageData]; 
     if (image) { 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       [imageCache setObject:image forKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]]; 
       cell.image.image = image; 
       [cell setNeedsLayout]; 
      }); 
     } 
    }); 
} 
+0

謝謝,這是工作。但只要圖像加載和當我開始按任何圖像,圖像開始隨機 –

+0

使用NSCache保存圖像和重用,否則你必須每次下載圖像爲什麼圖像隨機變化。 – Jeyamahesan

+0

感謝您的幫助 –