我正在尝试加载视频,在其上添加动画,然后将其导出, 但动画永远不会在导出的视频中开始播放。 它只是按原样显示图像“dogge_icon.png”。
我尝试了不同类型的动画,不确定我做错了什么。 任何帮助将不胜感激。
代码:
-(void) createCompositionWithPicture {
AVMutableComposition* composition = [AVMutableComposition composition];
NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
NSLog(@"Path: %@", videoPath);
NSURL *videoURL = [[NSURL alloc] initFileURLWithPath:videoPath];
AVURLAsset *videoAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
AVMutableCompositionTrack *videoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];
// Create an AVMutableVideoCompositionLayerInstruction for the video track.
AVMutableVideoCompositionInstruction *mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration);
AVMutableVideoCompositionLayerInstruction *videolayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
// Setup video composition
AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.renderSize = CGSizeMake(videoTrack.naturalSize.width,videoTrack.naturalSize.height);
videoComposition.frameDuration = CMTimeMake(1, 30);
mainInstruction.layerInstructions = [NSArray arrayWithObject:videolayerInstruction];
videoComposition.instructions = [NSArray arrayWithObject:mainInstruction];
NSLog(@"Width: %f Height: %f", videoTrack.naturalSize.width, videoTrack.naturalSize.height);
// Setup animation layer
UIImage* image = [UIImage imageNamed:@"dogge_icon.png"];
CALayer *animationLayer = [CALayer layer];
animationLayer.frame = CGRectMake(0, 0, image.size.width, image.size.height);
[animationLayer setMasksToBounds:YES];
[animationLayer setContents: (id)image.CGImage];
// Add animation
CABasicAnimation *animation =
[CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.duration=5.0;
animation.autoreverses=YES;
animation.fromValue = [NSNumber numberWithFloat:1.0f];
animation.toValue = [NSNumber numberWithFloat:2.0f];
animation.repeatCount=10;
animation.beginTime = AVCoreAnimationBeginTimeAtZero;
[animationLayer addAnimation:animation forKey:@"scale"];
NSLog(@"animationLayer animations: %@", [animationLayer animationKeys]);
// Build layer hierarchy
CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];
parentLayer.frame = CGRectMake(0, 0, videoTrack.naturalSize.width, videoTrack.naturalSize.height);
videoLayer.frame = CGRectMake(0, 0, videoTrack.naturalSize.width, videoTrack.naturalSize.height);
[parentLayer addSublayer:videoLayer];
[parentLayer addSublayer:animationLayer];
videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool
videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
// Export
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
NSString *exportVideoPath = [STPFileUtilities getPathToFileIn: NSDocumentDirectory WithName: @"composition.mov"];
[STPFileUtilities deleteFileIfExists:exportVideoPath];
NSURL *exportURL = [NSURL fileURLWithPath:exportVideoPath];
exportSession.outputURL = exportURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
exportSession.videoComposition = videoComposition;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch (exportSession.status) {
case AVAssetExportSessionStatusFailed:{
NSLog(@"FAIL: %@", exportSession.error);
break;
}
case AVAssetExportSessionStatusCompleted: {
NSLog (@"SUCCESS");
}
};
}];
}
最佳答案
我不确定我做了什么。 我从头开始重写了整个代码并添加了
animation.removedOnCompletion = NO;
到所有动画,现在可以了。
关于ios - AV Foundation 动画永远不会开始播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21760034/