2016-05-31 227 views
0

我正在處理視頻的應用程序中工作。在這個應用程序中,我想通過使用UITextView來爲視頻添加文本。爲此我編程創建了UITextView,並將其添加爲UIView這樣的子視圖。將textView作爲視圖的子視圖並將此視圖添加到視頻

[self.view addSubview:textView]; 

但只要我這個觀點添加到視頻像this--

[parentLayer addSublayer:textView]; 

我得到的錯誤,如this--

主題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]; 
    }); 
}]; 
} 

任何幫助將不勝感激。

回答

1

您不能將UIView子類添加爲圖層實例的子圖層。嘗試

[parentLayer addSublayer:textView.layer]; 

由於添加子圖層時類型不匹配而導致訪問不良。所需類型的參數是UILayer子類,但是您使用UIView代替。

希望這會有所幫助。

+0

現在,我得到錯誤主題1:EXC_BAD_ACCESS(代碼= 0地址= 0x102006e4)於行: UIApplicationMain(ARGC,ARGV,零,NSStringFromClass([AppDelegate類] )); @ Doro –

+0

@NehaPurwar你的textview沒有框架。設置框架,然後重試。 – Doro

+0

我認爲你沒有仔細檢查我的代碼。我已經初始化文本視圖的框架:textView.frame = CGRectMake(0,0,282,210); –

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

希望這將有助於:)

+0

謝謝你的回答,但我想通過視頻添加文本通過textview。我以前通過CATextLayer完成。 –

+0

爲什麼只有textview? CATextLayer非常簡單可靠! – Lion

+0

我知道CATextLayer比TextView更可靠,但在這個文本視圖中,我想通過使用GestureRecognizer來執行縮放,旋轉等操作。 –

相關問題