Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用AVMutableVideoComposition导出CALayer_Ios_Ipad_Avfoundation - Fatal编程技术网

Ios 使用AVMutableVideoComposition导出CALayer

Ios 使用AVMutableVideoComposition导出CALayer,ios,ipad,avfoundation,Ios,Ipad,Avfoundation,我试图通过视频导出图像,但当导出完成时,我会得到一个带有图像的黑屏视频。 下面是设置合成的代码: UIImage *myImage = [UIImage imageNamed:@"block.png"]; CALayer *aLayer = [CALayer layer]; aLayer.frame = CGRectMake(100, 100, 40, 40); aLayer.contents = (id)myImage.CGImage ; NSString *urlStr = [[NSBund

我试图通过视频导出图像,但当导出完成时,我会得到一个带有图像的黑屏视频。 下面是设置合成的代码:

UIImage *myImage = [UIImage imageNamed:@"block.png"];
CALayer *aLayer = [CALayer layer];
aLayer.frame = CGRectMake(100, 100, 40, 40);
aLayer.contents = (id)myImage.CGImage ;
NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"video-1" ofType:"mp4"];
NSURL *url = [NSURL fileURLWithPath:urlStr];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
cmp = [[AVMutableComposition alloc] init] ;  
AVMutableCompositionTrack *trackA = [cmp addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
NSError *error = nil ;
AVAssetTrack *sourceVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
[trackA insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset duration]) ofTrack:sourceVideoTrack atTime:kCMTimeZero error:&error] ;
animComp = [[AVMutableVideoComposition videoComposition] retain] ;
animComp.renderSize = CGSizeMake(640, 480);
animComp.frameDuration = CMTimeMake(1,30);
animComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithAdditionalLayer:aLayer asTrackID:2];
AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, [asset duration]);
AVMutableVideoCompositionLayerInstruction* layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:trackA];
[layerInstruction setTrackID:2];
[layerInstruction setOpacity:1.0 atTime:kCMTimeZero ];
instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction] ;
animComp.instructions = [NSArray arrayWithObject:instruction];
要导出电影,我使用以下代码:

-(IBAction) exportMovie:(id)sender{
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *tempPath = [docPaths objectAtIndex:0];
NSLog(@"Temp Path: %@",tempPath);

NSString *fileName = [NSString stringWithFormat:@"%@/output-anot.MOV",tempPath];
NSFileManager *fileManager = [NSFileManager defaultManager] ;
if([fileManager fileExistsAtPath:fileName ]){
    NSError *ferror = nil ;
    BOOL success = [fileManager removeItemAtPath:fileName error:&ferror];
}

NSURL *exportURL = [NSURL fileURLWithPath:fileName];

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:cmp presetName:AVAssetExportPresetHighestQuality]  ;
exporter.outputURL = exportURL ;
exporter.videoComposition = animComp ;
exporter.outputFileType= AVFileTypeQuickTimeMovie ;
[exporter exportAsynchronouslyWithCompletionHandler:^(void){
    switch (exporter.status) {
        case AVAssetExportSessionStatusFailed:{
            NSLog(@"Fail");
            break;
        }
        case AVAssetExportSessionStatusCompleted:{
            NSLog(@"Success");
            break;
        }

        default:
            break;
    } 
}];
}


我是错过了sthg还是做错了什么。

是的,我看到了问题,谢谢你的代码!我一直在尝试这样做,我只是复制粘贴(我是一个noob),但我在编辑我的问题代码时发现了您的问题,这一行

NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"video-1.mp4" ofType:nil];
这是错误的,有趣的一点,无论如何,把它改成这个

NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"video-1" ofType:@"mp4"];
这应该是唯一的问题。它是黑色的原因是因为它没有合适的电影