CMTime中fps的scale理解和UIPanGestureRecorgnize的手势记录

Jan 9, 2017

发表于(2016-11-29 10:39:20)

视频帧处理有一点要明确,
CMTimeMakeWithSeconds(
Float64 seconds,
int32_t preferredTimescale)
这里seconds是第几次,preferredTimescale为片,
比如我要每秒一帧图片从视频取出来
`AVAssetImageGenerator assetImageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:self.viewModel.asset];
CGFloat scale = KScreenWidth
1.f/MAXImageShowInCollection/80;//collectionFlowSize width/height
assetImageGenerator.maximumSize = CGSizeMake(200scale,200);
assetImageGenerator.appliesPreferredTrackTransform = TRUE;
Float64 duration = CMTimeGetSeconds(self.viewModel.asset.duration);
NSMutableArray
timeArr = [[NSMutableArray alloc]initWithCapacity:0];
if (self.segmentedControl.selectedSegmentIndex) {
for (int i=0; i<8; i++) {//MIN(duration, 60)
[timeArr addObject:[NSValue valueWithCMTime:CMTimeMakeWithSeconds(i, duration/8)]];
}
}else{
for (int i=0; i<floor(duration); i++) {
[timeArr addObject:[NSValue valueWithCMTime:CMTimeMakeWithSeconds(i, 1)]];
}
}
[assetImageGenerator generateCGImagesAsynchronouslyForTimes:timeArr completionHandler:
^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError error)
{
@strongify(self);
NSLog(@”actual got image at time:%f”, CMTimeGetSeconds(actualTime));
if (image)
{
UIImage
img = [UIImage imageWithCGImage:image];
[self.imageArray addObject:img];
if (self.imageArray.count==timeArr.count) {
dispatch_async(dispatch_get_main_queue(), ^{
@strongify(self);
[self.collection reloadData];
});
}
}

}];`

此处参考了美拍的如果取10s以内的视频,则都是以1s一片取图片CMTimeMakeWithSeconds(i, 1),如果为1分钟或者美拍的那种5分钟,则就取8帧,总共就8帧CMTimeMakeWithSeconds(i, duration/8)。

UIPanGestureRecognize在手势滑动的判断
我犯得错误是使用坐标对坐标,两个会变动的参数判断,这里给自己提一个醒!!!
每次对其内部判断,都是在判断时,对[x locationInView:trimView]判断,对目前你的手在区域内位置判断,不然会狂闪。
[[leftPanGesture rac_gestureSignal] subscribeNext:^(UIPanGestureRecognizer * x) { @strongify(self); if (x.state == UIGestureRecognizerStateBegan) { }else if (x.state == UIGestureRecognizerStateChanged) { if ((rightPullView.centerX-[x locationInView:trimView].x)>self.miniTwoLineLength) { }else{ } }
道理都懂,有时就会犯迷糊,谨记