ios – 将图像添加到视频但它被扭曲了

ios – 将图像添加到视频但它被扭曲了,第1张

概述我有一个UIView(见照片)和一个视频(见视频),我想将该视图导出到图像(参见“UIView to UI Image”代码)并添加到视频(参见“添加UIImage到视频”代码). 但我的结果并不好(见结果). 请帮帮我这个问题,谢谢! 照片 视频 结果 UIView到UIImage UIGraphicsBeginImageContextWithOptions(view.bounds.size, 我有一个UIVIEw(见照片)和一个视频(见视频),我想将该视图导出到图像(参见“UIVIEw to UI Image”代码)并添加到视频(参见“添加UIImage到视频”代码).
但我的结果并不好(见结果).
请帮帮我这个问题,谢谢!

照片

视频

结果

UIVIEw到UIImage

UIGraphicsBeginImageContextWithOptions(vIEw.bounds.size,vIEw.opaque,0.0);[vIEw.layer renderInContext:UIGraphicsGetCurrentContext()];UIImage * img = UIGraphicsGetimageFromCurrentimageContext();UIGraphicsEndImageContext();

将UIImage添加到视频中

- (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 – 将图像添加到视频但它被扭曲了所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1029792.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存