简体   繁体   English

在iOS中将视频裁剪成圆圈?

[英]Crop Video Into Circle in iOS?

I am trying to crop a video, that has been taken with an iPhone camera and then place it on top of a UIImageView. 我正在尝试裁剪视频,这是用iPhone相机拍摄的,然后将其置于UIImageView之上。 I have been following this SO Question How to crop a video to a circle in iOS?. 我一直在关注这个问题如何在iOS中将视频裁剪为圆圈? And I can now take the video and put it on top of another perviously recorded video. 我现在可以拍摄视频并将其置于另一个以前录制过的视频之上。 Now I want to have the background be a image and the foreground be the cropped video. 现在我希望背景为图像,前景为裁剪视频。 My main issue right now is getting video cropped in the part I want cropped. 我现在的主要问题是在我想裁剪的部分裁剪视频。 I cannot post all the code here but here is where the github repo is and the class that does the modifying is called CustomVideoCompositor.m https://github.com/mayoff/stackoverflow-28258270-video-in-oval-on-video/tree/master/video . 我不能在这里发布所有代码,但这里是github repo的地方,修改的类叫做CustomVideoCompositor.m https://github.com/mayoff/stackoverflow-28258270-video-in-oval-on-video / tree / master / video And I am having trouble editing it into the circle I want I want it to be a oval that is in the bottom half and higher than wider. 我无法将其编辑到我想要的圆圈中,我希望它是一个椭圆形,位于下半部分,高于宽部分。

EDIT 编辑

I want to make the cut so that only things in this part of the rounded rectangle would be cropped and available. 我想制作切割,以便只有圆角矩形的这一部分中的东西才会被裁剪和可用。

在此输入图像描述

You'll need to use AVPlayer as this enables you to play several videos back at once or have your video appear on top of another view. 您需要使用AVPlayer,因为这样您就可以一次播放多个视频,或者让您的视频显示在另一个视图的顶部。 Also, by using AVPlayer and an AVPlayerLayer its easy to then make the video appear circular. 此外,通过使用AVPlayer和AVPlayerLayer,它很容易使视频显示为圆形。 (Here's a good tutorial on it to learn more: http://jacopretorius.net/2013/02/playing-video-in-ios.html ) (这是一个很好的教程,可以了解更多信息: http//jacopretorius.net/2013/02/playing-video-in-ios.html

Here's the code (for Objective-C): 这是代码(对于Objective-C):

In your view controller .h file: 在您的视图控制器.h文件中:

#import <AVFoundation/AVFoundation.h>

Then in viewDidLoad : 然后在viewDidLoad

[super viewDidLoad];

// Set up the image view first (the imageView is an IBOutlet)
self.imageView.image = [UIImage imageNamed:@"image"];
self.imageView.alpha = 0.2;

NSURL *url = [[NSBundle mainBundle] URLForResource:@"video" withExtension:@"mp4"];
AVPlayer *player = [AVPlayer playerWithURL:url];

CGFloat diameter = MIN(self.view.frame.size.width, self.view.frame.size.height) * 0.8;

AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:player];
layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
layer.frame = CGRectMake((self.view.frame.size.width - diameter) / 2,
                         (self.view.frame.size.height - diameter) / 2,
                         diameter, diameter);

layer.cornerRadius = diameter / 2;
layer.masksToBounds = YES;

// Put the AVPlayerLayer on top of the image view.
[self.view.layer addSublayer:layer];

[player play];

If you want only a visual effect of cropped video, without modification of video file, you can simply add a mask to your video layer. 如果您只需要裁剪视频的视觉效果,而无需修改视频文件,则只需在视频图层中添加蒙版即可。 I mean CALayer mask property. 我的意思是CALayer mask属性。

You can create circle mask like this: draw black rectangle, then draw transparent circle on it. 您可以像这样创建圆形蒙版:绘制黑色矩形,然后在其上绘制透明圆圈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM