将textView添加为视图的子视图,并将此视图添加到视频

时间:2016-05-31 10:56:33

标签: ios objective-c

我正在处理视频处理的应用程序。在此应用程序中,我想使用UITextView添加带有视频的文本。为此,我以编程方式创建了UITextView并将其添加为UIView的子视图。 / p>

[self.view addSubview:textView];

但是,只要我将此视图添加到视频中这样 -

[parentLayer addSublayer:textView];

我收到这样的错误 -

  

线程1:EXC_BAD_ACCESS(代码= 1地址= 0x18)

请帮助我获取此信息。以下是我迄今为止为实现此目的所做的代码。

-(void)displayTextInVideo
 {
 if (!self.videoAsset) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please Load a Video Asset First"
                                                   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    return;
}
AVMutableComposition* mixComposition = [AVMutableComposition composition];

AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo  preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *clipVideoTrack = [[self.videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, self.videoAsset.duration)
                               ofTrack:clipVideoTrack
                                atTime:kCMTimeZero error:nil];

[compositionVideoTrack setPreferredTransform:[[[self.videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]];

CALayer *aLayer = [CALayer layer];
aLayer.frame = CGRectMake(0, 0, 30, 30); //Needed for proper display. We are using the app icon (57x57). If you use 0,0 you will not see it
aLayer.opacity = 0.75; //Feel free to alter the alpha here
//Create layer with watermark text instead of image
CGSize videoSize = [self.videoAsset naturalSize];
UITextView *textView =[[UITextView alloc]init];
textView.frame=CGRectMake(0,0,282,210);
[textView setReturnKeyType:UIReturnKeyDone];
[self.view addSubview:textView];
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];
[parentLayer addSublayer:textView]; //ONLY IF WE ADDED TEXT
//Create composition
AVMutableVideoComposition* videoComp = [AVMutableVideoComposition videoComposition];
videoComp.renderSize = videoSize;
videoComp.frameDuration = CMTimeMake(1, 30);
videoComp.animationTool = [AVVideoCompositionCoreAnimationTool      videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
//Create instruction to insert layer
AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, [mixComposition duration]);
AVAssetTrack *videoTrack = [[mixComposition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVMutableVideoCompositionLayerInstruction* layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction];
videoComp.instructions = [NSArray arrayWithObject: instruction];

NSString *destPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"NewwatermarkVideo.mp4"];
NSURL *exportUrl = [NSURL fileURLWithPath:destPath];



if ([[NSFileManager defaultManager] fileExistsAtPath:destPath])
{
    [[NSFileManager defaultManager] removeItemAtPath:destPath error:nil];
}



AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition
                                                                  presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL=exportUrl;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;
exporter.videoComposition = videoComp;
[exporter exportAsynchronouslyWithCompletionHandler:^{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self exportDidFinish:exporter];
    });
}];
}

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

您不能将UIView子类添加为图层实例的子图层。尝试

[parentLayer addSublayer:textView.layer];

由于添加子图层时类型不匹配,您的访问权限很差。所需类型的参数是UILayer子类,但您使用的是UIView。

希望这有帮助。

答案 1 :(得分:1)

 CGSize videoSize; //vedio size goes here

CATextLayer *titleLayer = [CATextLayer layer];
titleLayer.string = @"Text goes here";

titleLayer.fontSize = videoSize.height / 6;

titleLayer.alignmentMode = kCAAlignmentCenter;
titleLayer.bounds = CGRectMake(0, 0, videoSize.width, videoSize.height / 6); //You may need to adjust this for proper display

将此标题图层添加到parentLayer

希望这会有所帮助:)