集合视图不显示图像

时间:2016-09-08 00:59:30

标签: xcode uicollectionview

我正在尝试通过PHP在Collection View中加载图像,如下面的代码所示;

@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;

}

我的问题是图像没有显示在单元格中,所有的Collection View都是黑色的。 我可以看到控制台中的信息显示,我可以看到图像链接。已设置集合视图单元格,并在“属性”检查器中将标识符设置为“myCell”。

我很抱歉我的解释,我希望有人能帮助我如何展示图像。

提前致谢。

1 个答案:

答案 0 :(得分:0)

请求tableview内的URL应该是异步的,因为异步任务必须使用GCD (Grand Central Dispatch)。使用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];
            });
        }
    });
}