- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建像 Vine 这样的应用程序。我有一个 Collection View ,其中每个项目都包含一个带有 AVPlayerLayer
和 AVPlayer
实例的 View ,并且相应的 avplayer 已附加到图层。
一切正常。
但我想知道,如果我对 Collection View 中包含的每个 AVPlayerLayer
只使用一个 AVPlayer
实例,也许会更好。如果我想显示下一个项目,我将从当前项目的图层中删除 AVPlayer
实例,并将其移动到下一个项目的图层。毕竟我打电话 [videoPlayer ReplaceCurrentItemWithPlayerItem:newItem];
也许有人知道更好的解决方案来获得与 Vine 应用程序相同的性能。
最佳答案
这是我在 Collection View 中执行此操作的方法:
//
// ViewController.m
// VideoWall
//
// Created by James Alan Bush on 6/13/16.
// Copyright © 2016 James Alan Bush. All rights reserved.
//
#import "ViewController.h"
#import "AppDelegate.h"
static NSString *kCellIdentifier = @"Cell Identifier";
@interface ViewController () {
dispatch_queue_t dispatchQueueLocal;
}
@end
@implementation ViewController
- (id)initWithCollectionViewLayout:(UICollectionViewFlowLayout *)layout
{
if (self = [super initWithCollectionViewLayout:layout])
{
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCellIdentifier];
dispatchQueueLocal = dispatch_queue_create( "local session queue", DISPATCH_QUEUE_CONCURRENT );
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.collectionView setDataSource:self];
[self.collectionView setContentSize:CGSizeMake(AppDelegate.sharedAppDelegate.width, AppDelegate.sharedAppDelegate.height)];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[super dealloc];
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return AppDelegate.sharedAppDelegate.assetsFetchResults.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier forIndexPath:indexPath];
[CATransaction begin];
[CATransaction setCompletionBlock:^{
cell.contentView.layer.sublayers = nil;
dispatch_release(dispatchQueueLocal);
}];
dispatch_retain(dispatchQueueLocal);
dispatch_async( dispatchQueueLocal, ^{
[self drawPlayerLayerForCell:cell atIndexPath:indexPath];
});
[CATransaction commit];
return cell;
}
- (void)drawPlayerLayerForCell:(UICollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
void (^drawPlayerLayer)(UICollectionViewCell*, NSIndexPath*) = ^(UICollectionViewCell* cell, NSIndexPath* indexPath) {
[AppDelegate.sharedAppDelegate.imageManager requestPlayerItemForVideo:AppDelegate.sharedAppDelegate.assetsFetchResults[indexPath.item] options:nil resultHandler:^(AVPlayerItem * _Nullable playerItem, NSDictionary * _Nullable info) {
dispatch_async(dispatch_get_main_queue(), ^{
if(![[info objectForKey:PHImageResultIsInCloudKey] boolValue]) {
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:[AVPlayer playerWithPlayerItem:playerItem]];
[playerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[playerLayer setBorderColor:[UIColor whiteColor].CGColor];
[playerLayer setBorderWidth:1.0f];
[playerLayer setFrame:cell.contentView.bounds];
[cell.contentView.layer addSublayer:playerLayer];
[(AVPlayer *)playerLayer.player play];
} else {
[AppDelegate.sharedAppDelegate.imageManager requestImageForAsset:AppDelegate.sharedAppDelegate.assetsFetchResults[indexPath.item]
targetSize:CGSizeMake(AppDelegate.sharedAppDelegate.flowLayout.itemSize.width, AppDelegate.sharedAppDelegate.flowLayout.itemSize.height)
contentMode:PHImageContentModeAspectFill
options:nil
resultHandler:^(UIImage *result, NSDictionary *info) {
dispatch_async(dispatch_get_main_queue(), ^{
cell.contentView.layer.contents = (__bridge id)result.CGImage;
});
}];
}
});
}];
};
drawPlayerLayer(cell, indexPath);
}
@end
AppDelegate 实现文件如下所示:
//
// AppDelegate.m
// VideoWall
//
// Created by James Alan Bush on 6/13/16.
// Copyright © 2016 James Alan Bush. All rights reserved.
//
#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
+ (AppDelegate *)sharedAppDelegate
{
return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch
self.width = [[UIScreen mainScreen] bounds].size.width / 2.0;
self.height = [[UIScreen mainScreen] bounds].size.height / 4.0;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [self viewController];
self.window.rootViewController.view = self.viewController.view;
[self.window makeKeyAndVisible];
return YES;
}
- (ViewController *)viewController {
ViewController *c = self->_viewController;
if (!c) {
c = [[ViewController alloc] initWithCollectionViewLayout:[self flowLayout]];
[c.view setFrame:[[UIScreen mainScreen] bounds]];
self->_viewController = c;
}
return c;
}
- (UICollectionViewFlowLayout *)flowLayout {
UICollectionViewFlowLayout *v = self->_flowLayout;
if (!v) {
v = [UICollectionViewFlowLayout new];
[v setItemSize:CGSizeMake(AppDelegate.sharedAppDelegate.width, AppDelegate.sharedAppDelegate.height)];
[v setSectionInset:UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)];
[v setMinimumLineSpacing:0.0];
[v setMinimumInteritemSpacing:0.0];
[v setEstimatedItemSize:CGSizeMake(AppDelegate.sharedAppDelegate.width, AppDelegate.sharedAppDelegate.height)];
self->_flowLayout = v;
}
return v;
}
- (PHCachingImageManager *)imageManager {
PHCachingImageManager *i = self->_imageManager;
if (!i) {
i = [[PHCachingImageManager alloc] init];
self->_imageManager = i;
}
return i;
}
- (PHFetchResult *)assetsFetchResults {
PHFetchResult *i = self->_assetsFetchResults;
if (!i) {
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumVideos options:nil];
PHAssetCollection *collection = smartAlbums.firstObject;
if (![collection isKindOfClass:[PHAssetCollection class]])
return nil;
PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
i = [PHAsset fetchAssetsInAssetCollection:collection options:allPhotosOptions];
self->_assetsFetchResults = i;
}
return i;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
这是您唯一需要的两个文件;无 NIB/XIB。
关于ios - 每个 AVPlayerLayer 一个 AVPlayer 与多个 AVPlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19984428/
我使用以下命令设置 AVPlayerLayer: audioPlayer = [[AVPlayer alloc]initWithURL:[NSURL fileURLWithPath:f
我有一个自定义的UITableViewCell,它显示图像或视频。如果有视频,它会在加载并准备播放视频时首先下载并显示该视频的缩略图。 一切正常,问题是我获取缩略图的比例,将其存储在数据库中,当我下载
我有一个令人困惑的问题。我在我的类中声明了这些变量: var audioPlayer = AVPlayer() var videoPlayer = AVPlayer() var videoLayer
我一直在尝试让 AVPlayerLayer 在我的应用程序中播放从互联网上下载的视频。然而,尽管有所有在线帮助,我似乎无法让它工作。这是代码: import UIKit import CoreGrap
注意:我接受 Xamarin.iOS、Objective-C 或 Swift 中的任何答案。如果您需要帮助阅读 Xamarin 代码,我可以为您解决这个问题,但考虑到此处代码的“复杂性”,应该没问题。
我有一个奇怪的问题。我有一个表格 View ,它的单元格包含一个视频。我将 AVPlayerLayer 设置为拉伸(stretch)到单元格的大小,但是当 tableview 首次加载时,它不会这样做
我试图让 AVPlayerLayer 填充单元格空间,但是通过设置 frame 属性,它不会改变它的大小。 在我的自定义单元格文件中: override init(style: UITableVie
我正在使用 AVPlayerLayer 播放视频。视频没有声音。但是,如果用户正在后台播放音乐并且显示了 AVPlayerLayer,则音乐会停止。如果用户向上滑动并点击播放,视频将停止。 我什至已经
我有一个带有 UIImageView 的旋转木马对象,我试图在其中对 AvPlayerLayer 进行子层处理。 虽然它没有填满 _videoView 的整个边界,但它工作得很好,它的高度看起来更小,
我试图在 有时 工作的 AVPlayerLayer 中显示本地录制的视频。我可以听到录制视频的声音,但看不到视频。有时视频和音频都在工作,有时只有音频。 我已经尝试使用 AVPlayerLayer 和
每当我更改 AVPlayerLayer 的框架时,视频不会立即调整大小,而是根据新大小进行动画处理。 例如:我将帧从 (0, 0, 100, 100) 更改为 (0, 0, 400, 400), Vi
我有两种观点 其中一个是AVPlayerLayer的容器,如下所示 containerView.layer.addSublayer(playerLayer) containerView 有自己关于 s
我正在使用 AVPlayerLayer 在我的 UIViewController 实例的后台播放视频。当我在 iPhone 上启动我的应用程序时,我注意到放大镜的显示方式有误。视频规范:H.264、A
我正在开发一个应用程序,其中提要中有循环播放的视频(在表格 View 单元格内)。可以同时存在多个视频,但可能只有一个正在播放。我在后台创建 AVPlayer,并将它们的 playerLayer 作为
我似乎无法从我拥有的 URL 中获取视频的高度。每次我记录playerLayer.videoRect.bounds.size.height返回0.000。关于如何根据视频调整playerLayer的高
我看到很多人想要播放透明背景视频的帖子。这不是我在这里想要实现的目标。 我正在尝试设置整个图层的不透明度。下面是我如何设置图像层的不透明度。我尝试了相同的方法来设置视频层的不透明度,但当我尝试淡入淡出
在 Main.storyboard 窗口中添加了一个 View Controller 组件。 View as: Iphone8 选项在 Main.storyboard 窗口中被选中 TestViewC
我不想用 AVPlayerLayer 重现实时照片缩放效果。为此,我使用 VideoView: class VideoView: UIView { override class var lay
我正在尝试使用此代码显示视频: self.videoPlayer = AVPlayer.init(url: videoUrl as URL) let playerLayer = AVPlayerLay
我有一个位于 collectionView 单元中的 AVPlayer,它位于 collectionView 中,位于 collectionView 单元中。 总而言之,我的每个单元格中都有一个 Co
我是一名优秀的程序员,十分优秀!