gpt4 book ai didi

ios - 使用 CABasicAnimation animationWithKeyPath : @"contents" on CATextLayer to display dynamic overlay text on Video

转载 作者:行者123 更新时间:2023-11-29 12:14:03 26 4
gpt4 key购买 nike

我正在开发一款可向视频添加动态文本(如字幕)的应用程序。有许多类似的问题,但没有一个真正有一个有效的答案。我对使用“ContentAnimate”而不是“contents”作为基于 Synchronising image, text, and positioning with CoreAnimation 的 animationKeyPath 寄予厚望。但这对我不起作用。

我可以让文本作为叠加层显示在视频上,但动画不起作用 - 我总是看到来自 CATextLayer 的原始基本文本。以下是我如何设置字幕和覆盖:

        // 1 - Set up the text layer
CATextLayer *subtitle1Text = [[CATextLayer alloc] init];
[subtitle1Text setFont:@"Helvetica-Bold"];
//[subtitle1Text setString:[NSString stringWithFormat:@"%@: %@", bookmark.creatorMonkeyName, bookmark.info]];
[subtitle1Text setString:@"Place holder"];

[subtitle1Text setFontSize:36];
[subtitle1Text setFrame:CGRectMake(0, 0, videoSize.width, 100)];
[subtitle1Text setAlignmentMode:kCAAlignmentCenter];
[subtitle1Text setForegroundColor:[[UIColor whiteColor] CGColor]];


// 2 - The overlay
CALayer *overlayLayer = [CALayer layer];
[overlayLayer addSublayer:subtitle1Text];
overlayLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
[overlayLayer setMasksToBounds:YES];
overlayLayer.hidden = YES;

CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];
parentLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
videoLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);

[parentLayer addSublayer:videoLayer];
[parentLayer addSublayer:overlayLayer];
videoComp.renderSize = videoSize;
videoComp.frameDuration = CMTimeMake(1, (int32_t) fps);

这是我添加动画的方法:

            CABasicAnimation *showCommentAnimation2 = [CABasicAnimation animationWithKeyPath:@"contents"];

[CATransaction begin];
[CATransaction setDisableActions:YES];

overlayLayer.hidden = NO;
showCommentAnimation2.fromValue = @"Hello";
showCommentAnimation2.toValue = @"Goodbye";
showCommentAnimation2.beginTime = bookmark.relativeTimeStamp;
showCommentAnimation2.duration = commentTimeDisplayed;
[subtitle1Text addAnimation:showCommentAnimation2 forKey:[@(i) stringValue]]; //"i" is an iterator, This should allow multiple animations of the same type
[CATransaction setDisableActions:NO];
[CATransaction commit];

副标题弹出并消失 - 但我总是看到文本“占位符”,而不是“你好”或“再见”。

我使用以下方法将视频导出到相机胶卷:

        videoComp.animationTool = [AVVideoCompositionCoreAnimationTool
videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
_assetExport.videoComposition = videoComp;
_assetExport.outputFileType = @"public.mpeg-4";
_assetExport.outputURL = outputFileUrl;
[_assetExport exportAsynchronouslyWithCompletionHandler:
^(void ) {
if (AVAssetExportSessionStatusCompleted == _assetExport.status) {
DLog(@"AVAssetExportSessionStatusCompleted");
ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease];
DLog(@"About to copy video to camera roll");
//move video to camera roll

[library writeVideoAtPathToSavedPhotosAlbum:outputFileUrl completionBlock:^(NSURL *assetURL, NSError *error) {
[self removeWaitingScreen];
dispatch_async(dispatch_get_main_queue(), ^(void){
MPMoviePlayerViewController* theMovie = [[MPMoviePlayerViewController alloc] initWithContentURL: assetURL];
[_viewController presentMoviePlayerViewControllerAnimated:theMovie];

});
}];
} else{
// a failure may happen because of an event out of your control
// for example, an interruption like a phone call comming in
// make sure and handle this case appropriately
DLog(@"Error - Export Session Status: %ld", (long)_assetExport.status);
dispatch_async(dispatch_get_main_queue(), ^(void){
[self removeWaitingScreen];
[[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", NSStringFromClass([self class]))
message:NSLocalizedString(@"Video Creation Failed", nil)
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", nil)
otherButtonTitles: nil] autorelease] show];
});
}

[videoComp release];
}];

}];

有人能帮忙吗?

如果有更好的方法将字幕刻录到视频中,我愿意接受建议。

谢谢。

最佳答案

具有键路径 contents 的动画不能与字符串值(@"Hello"@"Goodbye")一起使用。层的 contents 属性包含图像,可手动设置或由层本身绘制。

只需设置文本层的 string 属性就足以产生隐式动画。但是,听起来您需要一个明确的动画来控制 beginTime。这样做的方法是使用 CATransition .参见 here举个例子。

关于ios - 使用 CABasicAnimation animationWithKeyPath : @"contents" on CATextLayer to display dynamic overlay text on Video,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32420650/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com