大佬教程收集整理的这篇文章主要介绍了ios – 将图像添加到视频但它被扭曲了,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
照片
UIView到UIImage
UIGraphicsBeginImageContextWithOptions(view.bounds.size,view.opaque,0.0); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
- (void)showImage:(UIImage*)img toCompostion:(AVMutableVideoComposition *)composition fromTime:(CGFloat)fromTime toTime:(CGFloat)toTime videoSize:(CGSizE)size { CALayer *parentLayer = [CALayer layer]; CALayer *viLayer = [CALayer layer]; [parentLayer addSublayer:viLayer]; parentLayer.frame = CGRectMake(0,size.width,size.height); viLayer.frame = CGRectMake(0,size.height); // 1 - set up the overlay CALayer *imgLayer = [CALayer layer]; [imgLayer setContents:(id)[img CGImage]]; imgLayer.frame = CGRectMake(50,50,img.size.width,img.size.height); // Oxy: bottom left [imgLayer setMasksToBounds:YES]; // 2 - set up the parent layer [parentLayer addSublayer:imgLayer]; // the code for the opacity animation which then removes the image imgLayer.opacity = 0.0; CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; [animation setDuration:0.1]; //duration [animation setFromValue:[NSnumber numberWithFloat:0.0]]; [animation setToValue:[NSnumber numberWithFloat:1.0]]; [animation setBeginTime:fromTime]; // time to show text [animation setRemovedOnCompletion:NO]; [animation setFillmode:kCAFillmodeForWARDs]; [imgLayer addAnimation:animation forKey:@"animateOpacity"]; animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; [animation setDuration:0.1]; //duration [animation setFromValue:[NSnumber numberWithFloat:1.0]]; [animation setToValue:[NSnumber numberWithFloat:0.0]]; [animation setBeginTime:toTime]; // time to show text [animation setRemovedOnCompletion:NO]; [animation setFillmode:kCAFillmodeForWARDs]; [imgLayer addAnimation:animation forKey:@"animateOpacity1"]; //// // 3 - apply magic composition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:viLayer inLayer:parentLayer]; }
********更新1 ********
如果我这样做就可以了:
(1)将uIGraphicsBeginImageContextWithOptions的比例从0.0改为1.0.
(2)将图像保存到文件中.
(3)等待0.01秒并回读该文件.
我不知道为什么需要保存到文件????
以上是大佬教程为你收集整理的ios – 将图像添加到视频但它被扭曲了全部内容,希望文章能够帮你解决ios – 将图像添加到视频但它被扭曲了所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。