Вот как вы можете воспроизводить 8 за раз, а затем воспроизводить еще 8 за один раз с помощью прокрутки:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier forIndexPath:indexPath];
// Enumerate array of visible cells' indexPaths to find a match
if ([self.collectionView.indexPathsForVisibleItems
indexOfObjectPassingTest:^BOOL(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
return (obj.item == indexPath.item);
}]) dispatch_async(dispatch_get_main_queue(), ^{
[self drawLayerForPlayerForCell:cell atIndexPath:indexPath];
});
return cell;
}
- (void)drawPosterFrameForCell:(UICollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
[self.imageManager requestImageForAsset:AppDelegate.sharedAppDelegate.assetsFetchResults[indexPath.item]
targetSize:AssetGridThumbnailSize
contentMode:PHImageContentModeAspectFill
options:nil
resultHandler:^(UIImage *result, NSDictionary *info) {
cell.contentView.layer.contents = (__bridge id)result.CGImage;
}];
}
- (void)drawLayerForPlayerForCell:(UICollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
cell.contentView.layer.sublayers = nil;
[self.imageManager requestPlayerItemForVideo:(PHAsset *)self.assetsFetchResults[indexPath.item] options:nil resultHandler:^(AVPlayerItem * _Nullable playerItem, NSDictionary * _Nullable info) {
dispatch_sync(dispatch_get_main_queue(), ^{
if([[info objectForKey:PHImageResultIsInCloudKey] boolValue]) {
[self drawPosterFrameForCell:cell atIndexPath:indexPath];
} else {
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:[AVPlayer playerWithPlayerItem:playerItem]];
[playerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[playerLayer setBorderColor:[UIColor whiteColor].CGColor];
[playerLayer setBorderWidth:1.0f];
[playerLayer setFrame:cell.contentView.layer.bounds];
[cell.contentView.layer addSublayer:playerLayer];
[playerLayer.player play];
}
});
}];
}
Обратите внимание, что метод drawPosterFrameForCell
помещает изображение, где видео не может быть воспроизведено, потому чтоон хранится в iCloud, а не на устройстве.