我用AVFoundation创建了一个类似于Snapchat的自定义相机.图片方面有效:拍照并显示图片.现在我正在尝试拍摄视频并显示视频.
我已经捕获了一个成功的视频文件,经过测试并与MPMoviePlayer配合使用,但无法在AVPlayer上播放.
- (void)takeVideoAction:(id)sender {
NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"output.mp4"];
NSFileManager *manager = [[NSFileManager alloc] init];
if ([manager fileExistsAtPath:outputPath]){
[manager removeItemAtPath:outputPath error:nil];
}
[movieFileOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:outputPath] recordingDelegate:self];
}
Run Code Online (Sandbox Code Playgroud)
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error{
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:outputFileURL options:nil];
[asset loadValuesAsynchronouslyForKeys:@[@"tracks"] completionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
NSError *error = nil;
if([asset statusOfValueForKey:@"tracks" error:&error] == AVKeyValueStatusFailed){
NSLog(@"error: %@", [error description]);
}
else{
self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
self.playerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:self.playerLayer]; …Run Code Online (Sandbox Code Playgroud) 我需要知道何时AVPlayer完全加载了视频资产,因此没有视频延迟/冻结.当我开始播放视频时,它播放一半然后冻结.如果我退出并再次打开,它将播放视频的其余部分,并将平滑地重复播放视频.首先我加载资产:
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
NSString *tracksKey = @"tracks";
[asset loadValuesAsynchronouslyForKeys:@[tracksKey] completionHandler:
^{
// Completion handler block.
dispatch_async(dispatch_get_main_queue(),
^{
NSError *error;
AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error];
if (status == AVKeyValueStatusLoaded) {
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];
// Set Key-Observers
}
});
}];
Run Code Online (Sandbox Code Playgroud)
我在项目上设置了三个关键观察者,并为玩家设置项目
[item addObserver:self forKeyPath:@"status"
options:NSKeyValueObservingOptionNew context:&ItemStatusContext];
[item addObserver:self forKeyPath:@"playbackLikelyToKeepUp"
options:NSKeyValueObservingOptionNew context:&playbackLikelyToKeepUp];
[item addObserver:self forKeyPath:@"playbackBufferFull"
options:NSKeyValueObservingOptionNew context:&playbackBufferFull];
self.player = [AVPlayer playerWithPlayerItem:item];
Run Code Online (Sandbox Code Playgroud)
接下来,我检查播放器/项目是否准备好播放,以及playBackLikelyToKeepUp是否为真.
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ((context == …Run Code Online (Sandbox Code Playgroud) 我有一个UICollectionView我正在加载多个图像.从我读过的内容来看,为了将正确的图像与每个单元格匹配,我需要子类化UIImageView并在那里获得图像.因为每次我collectionView reloadData,一些图像重复,它们都是乱序.但我不确定如何做到这一点,并没有找到任何教程.我正在使用Parse作为数据库.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
albumImageCell *cell = (albumImageCell *) [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[albumImageCell alloc]init];
}
PFObject *temp = [_dataArray objectAtIndex:indexPath.row];
PFFile *file = [temp objectForKey:@"imageThumbnail"];
if (![cell.hasImage isEqualToString:@"YES"]) {
dispatch_async(imageQueue, ^{
NSData *data = [file getData];
if (data) {
UIImage *image = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^(void){
cell.imageView.image = image;
cell.hasImage = @"YES";
});
}
});
}
return cell;
}
Run Code Online (Sandbox Code Playgroud) 我有两个collectionViews.一个collectionViewController和标题中的另一个collectionView.当我向下滚动collectionViewController时,标题collectionView的collectionView单元格消失.标头中还有一个segControl,用于更改collectionViewController单元格,这也使标题collectionView单元格消失.当控制器出现所有单元格都存在时,但是当我滚动选择segControl时,标题集合视图单元格消失.collectionViewController工作正常,只是标题中的那个搞砸了.
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (collectionView == self.collectionView) {
return [self.dataArray count];
}
return [self.groupArray count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if (collectionView != self.collectionView) {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"socialCell" forIndexPath:indexPath];
PFObject *group = [self.groupArray objectAtIndex:indexPath.row];
UILabel *label = (UILabel *) [cell viewWithTag:55];
label.text = [group objectForKey:@"Title"];
return cell;
}
userPostCell *postCell = (userPostCell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"postCell" forIndexPath:indexPath];
userPictureCell *pictureCell = (userPictureCell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"pictureCell" forIndexPath:indexPath];
userEventCell …Run Code Online (Sandbox Code Playgroud) ios ×4
avplayer ×2
asynchronous ×1
avasset ×1
avfoundation ×1
nsurl ×1
objective-c ×1
xcode ×1