Reputation: 516
Need to display Videos and images in UICollectionView.
How to display number of Videos and images in UICollectionView. I am using JSON for download assets from server.
Below is the code for download images from server :
NSDictionary *dictImage = [arrImages objectAtIndex:indexPath.item];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[dictImage objectForKey:@“imgURL"]]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
[cell.imgGallery setImage:[UIImage imageWithData:imgData]];
}];
Here is the example link for this screen.
Upvotes: 1
Views: 2296
Reputation: 427
For Image display, Use SDWebImage - Asynchronous image downloader.
#import <SDWebImage/UIImageView+WebCache.h>
[imageView sd_setImageWithURL:[NSURL
URLWithString:@"http://www.example.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
For Video display, Get placeHolder image from Video and Put Play icon on that videoImage cell.
AVURLAsset *asset1 = [[AVURLAsset alloc] initWithURL:_videoFileURL options:nil];
AVAssetImageGenerator *generate1 = [[AVAssetImageGenerator alloc] initWithAsset:asset1];
generate1.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
CMTime time = CMTimeMake(1, 2);
CGImageRef oneRef = [generate1 copyCGImageAtTime:time actualTime:NULL error:&err];
UIImage *one = [[UIImage alloc] initWithCGImage:oneRef];
imgViewVideo.image = one;
When click on Video open AVVideoPlayer
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc]init];
playerViewController.player = [[AVPlayer alloc]initWithURL:_videoFileURL];
[self presentViewController:playerViewController animated:YES completion:nil];
playerViewController.view.frame = self.view.frame;
[playerViewController.player play];
Upvotes: 0
Reputation: 1657
Load thumbnail images from SDWebImage
and on click of play button, play video with any video player.
Be careful when playing videos on cells, remove the previous video before playing the new one.
SDWebImage usage -
[cell.imgGallery sd_setImageWithURL:[NSURL
URLWithString:@"image_url"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
Upvotes: 0
Reputation: 19750
Personally here, I would not load the actual videos into a player in the cell. You should generate thumbnail images and use these with a play icon over the top.
Then when a user selects the video cell, you can present the video player over the top of the collection view.
There is a limit to the number of active AVPlayers you can use at one time and they would consume alot more memory and resources. Scrolling through a large list of cells containing AVPlayers would really slow down cell rendering and eventually would likely crash your application
Upvotes: 6