Neha Purwar Neha Purwar - 1 year ago 98
Objective-C Question

adding textView as a subview of a view and adding this view to video

I am working in an application in which i am dealing with video . In this application i want to add the text with the video by using the

UITextView
.For this i have created the
UITextView
programmatically and added it as a subview of the
UIView
like this.

[self.view addSubview:textView];


But as soon as i add this view to the video like this--

[parentLayer addSublayer:textView];


i am getting the error like this--


Thread 1:EXC_BAD_ACCESS (Code=1 Address=0x18)


please help me for getting this.Following is code that i have done so far for achieve this.

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


Any help will be appreciated.

Answer Source

You can not add UIView subclass as sublayer to layer instanse. Try

[parentLayer addSublayer:textView.layer];

You are getting bad access because of type mismatch while adding sublayer. The desired type of param is UILayer subclass, but you used UIView instead.

Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download