2016-09-08 56 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

感谢您的帮助 –