<i id='ijSfd'><tr id='ijSfd'><dt id='ijSfd'><q id='ijSfd'><span id='ijSfd'><b id='ijSfd'><form id='ijSfd'><ins id='ijSfd'></ins><ul id='ijSfd'></ul><sub id='ijSfd'></sub></form><legend id='ijSfd'></legend><bdo id='ijSfd'><pre id='ijSfd'><center id='ijSfd'></center></pre></bdo></b><th id='ijSfd'></th></span></q></dt></tr></i><div id='ijSfd'><tfoot id='ijSfd'></tfoot><dl id='ijSfd'><fieldset id='ijSfd'></fieldset></dl></div>

    1. <tfoot id='ijSfd'></tfoot>
      <legend id='ijSfd'><style id='ijSfd'><dir id='ijSfd'><q id='ijSfd'></q></dir></style></legend>

      <small id='ijSfd'></small><noframes id='ijSfd'>

          <bdo id='ijSfd'></bdo><ul id='ijSfd'></ul>
      1. 使用 AVVideoCompositionCoreAnimationTool 在 CALayer 中将视频与静态图像混合

        时间:2023-10-03
          <tbody id='NIV6f'></tbody>

            <bdo id='NIV6f'></bdo><ul id='NIV6f'></ul>

          • <tfoot id='NIV6f'></tfoot>
            • <small id='NIV6f'></small><noframes id='NIV6f'>

              1. <legend id='NIV6f'><style id='NIV6f'><dir id='NIV6f'><q id='NIV6f'></q></dir></style></legend>
              2. <i id='NIV6f'><tr id='NIV6f'><dt id='NIV6f'><q id='NIV6f'><span id='NIV6f'><b id='NIV6f'><form id='NIV6f'><ins id='NIV6f'></ins><ul id='NIV6f'></ul><sub id='NIV6f'></sub></form><legend id='NIV6f'></legend><bdo id='NIV6f'><pre id='NIV6f'><center id='NIV6f'></center></pre></bdo></b><th id='NIV6f'></th></span></q></dt></tr></i><div id='NIV6f'><tfoot id='NIV6f'></tfoot><dl id='NIV6f'><fieldset id='NIV6f'></fieldset></dl></div>
                  本文介绍了使用 AVVideoCompositionCoreAnimationTool 在 CALayer 中将视频与静态图像混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试将来自相机的视频与静态图像(水印)混合.

                  I am trying to mix video, coming from the camera with a static image (watermarking).

                  我检查了这里的问题/答案和一些示例,包括 Apple 的 WWDC AVEditDemo,并以以下代码结束.不幸的是,导出的视频不包含带有图像的图层.有什么想法吗?

                  I have checked around questions/answers here and some examples, including WWDC AVEditDemo from Apple and ended with the following code. Unfortunately, the exported video does not contain the layer with the image. Any ideas?

                  - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
                  
                  /// incoming video
                  NSURL *videoURL = [info valueForKey:UIImagePickerControllerMediaURL];
                  
                  /// UIImage into CALayer
                  UIImage *myImage = [UIImage imageNamed:@"m1h.png"];
                  CALayer *aLayer = [CALayer layer];
                  aLayer.contents = (id)myImage.CGImage;
                  
                  AVURLAsset* url = [AVURLAsset URLAssetWithURL:videoURL options:nil];
                  AVMutableComposition *videoComposition = [AVMutableComposition composition];
                  NSError *error;
                  NSFileManager *fileManager = [NSFileManager defaultManager];
                  
                  AVMutableCompositionTrack *compositionVideoTrack = [videoComposition  addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
                  AVAssetTrack *clipVideoTrack = [[url tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
                  [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [url duration])  ofTrack:clipVideoTrack atTime:kCMTimeZero error:&error];
                  
                  AVMutableVideoComposition* videoComp = [[AVMutableVideoComposition videoComposition] retain];
                  videoComp.renderSize = CGSizeMake(640, 480);
                  videoComp.frameDuration = CMTimeMake(1, 30);
                  videoComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithAdditionalLayer:aLayer asTrackID:2];
                  
                  
                  /// instruction
                  AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
                  instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30) );
                  AVMutableVideoCompositionLayerInstruction* layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack];
                  instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction];
                  videoComp.instructions = [NSArray arrayWithObject: instruction];
                  
                  /// outputs
                  NSString *filePath = nil;
                  filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                  filePath = [filePath stringByAppendingPathComponent:@"temp.mov"]; 
                  NSLog(@"exporting to: %@", filePath);
                  if ([fileManager fileExistsAtPath:filePath]) 
                  {
                          BOOL success = [fileManager removeItemAtPath:filePath error:&error];
                          if (!success) NSLog(@"FM error: %@", [error localizedDescription]);
                  }
                  
                  /// exporting
                  AVAssetExportSession *exporter;
                  exporter = [[AVAssetExportSession alloc] initWithAsset:videoComposition presetName:AVAssetExportPresetHighestQuality] ;
                  exporter.videoComposition = videoComp;
                  exporter.outputURL=[NSURL fileURLWithPath:filePath];
                  exporter.outputFileType=AVFileTypeQuickTimeMovie;
                  
                  [statusLabel setText:@"processing..."];
                  
                  [exporter exportAsynchronouslyWithCompletionHandler:^(void){
                      switch (exporter.status) {
                          case AVAssetExportSessionStatusFailed:
                              NSLog(@"exporting failed");
                              break;
                          case AVAssetExportSessionStatusCompleted:
                              NSLog(@"exporting completed");
                              UISaveVideoAtPathToSavedPhotosAlbum(filePath, self, @selector(video:didFinishSavingWithError:contextInfo:), NULL);
                              break;
                          case AVAssetExportSessionStatusCancelled:
                              NSLog(@"export cancelled");
                              break;
                      }
                  }];
                  

                  }

                  推荐答案

                  在玩了之后,除了上面的代码之外,我最终得到了这样的东西,还将使用的方法更改为 videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:

                  After playing around I ended up with something like this in a addition to the above code, also changing the used method to videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:

                  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:aLayer];
                  videoComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
                  

                  希望它对某人有所帮助.

                  Hope it helps somebody.

                  这篇关于使用 AVVideoCompositionCoreAnimationTool 在 CALayer 中将视频与静态图像混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:升级到 Xcode 4 后出现缺少文件警告 下一篇:如何在 Xcode 4.2 中启用 ARC 项目范围

                  相关文章

                    <bdo id='qCpym'></bdo><ul id='qCpym'></ul>

                  1. <i id='qCpym'><tr id='qCpym'><dt id='qCpym'><q id='qCpym'><span id='qCpym'><b id='qCpym'><form id='qCpym'><ins id='qCpym'></ins><ul id='qCpym'></ul><sub id='qCpym'></sub></form><legend id='qCpym'></legend><bdo id='qCpym'><pre id='qCpym'><center id='qCpym'></center></pre></bdo></b><th id='qCpym'></th></span></q></dt></tr></i><div id='qCpym'><tfoot id='qCpym'></tfoot><dl id='qCpym'><fieldset id='qCpym'></fieldset></dl></div>

                  2. <tfoot id='qCpym'></tfoot>

                    <small id='qCpym'></small><noframes id='qCpym'>

                  3. <legend id='qCpym'><style id='qCpym'><dir id='qCpym'><q id='qCpym'></q></dir></style></legend>