需要在uicollectionview中显示视频和图像

时间:2014-05-08 10:27:46

标签: ios objective-c

如何在UICollectionview中显示视频和图像 我为此视图提供了URL:

http://exarcplus.com/check.png

使用JSON从服务器获取图像和视频。

从服务器获取图像的代码:

NSDictionary *appsdict = [array_all objectAtIndex:indexPath.item];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[appsdict objectForKey:@"url"]]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        [cell.image_gallery setImage:[UIImage imageWithData:data]];
    }];
////////but don't know how to fetch videos from server

1 个答案:

答案 0 :(得分:0)

您需要将AsyncImageView类导入到项目中,并使用给定标记在集合视图单元格中获取图像视图和标签,如下所示:

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [your array count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"MyGridViewCell";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];


    AsyncImageView *imageView = (AsyncImageView *)[cell viewWithTag:1];

    [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:imageView];
    imageView.imageURL =[NSURL URLWithString:[your array objectAtIndex:indexPath.row][@"product_image"]];

    UILabel *titleLabel  = (UILabel *)[cell viewWithTag:2];

    NSString *nameText = [NSString stringWithFormat:@"%d. %@",indexPath.row+1,[arrayforlistt objectAtIndex:indexPath.row][@"product_name"]];
    titleLabel.text= nameText;
    return cell;

}

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

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // enter your code here if you want to perform any action on the did select of the project
}