iphone - 如何将 iPhone 摄像头拍摄的视频显示在屏幕上而不保存?

标签 iphone ios video avfoundation

我是 iOS 和多媒体开发新手,我正在尝试从 iPhone 的相机捕获视频并将其显示在屏幕上,而不将其保存在内存中。

到目前为止,我借助网上获得的示例代码编写了以下代码:

头文件:

//  ViewController.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@class AVCamCaptureManager, AVCamPreviewView, AVCaptureVideoPreviewLayer;
@interface ViewController : UIViewController <UIImagePickerControllerDelegate>

@property (weak, nonatomic) IBOutlet UIView *previewView;

- (IBAction)StartCapture:(id)sender;
- (void)setCaptureSession;
@end

实现文件

//  ViewController.m

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) AVCaptureSession *captureSession;
@property (nonatomic, strong) AVCaptureMovieFileOutput *captureOutput;
@property (nonatomic, weak) AVCaptureDeviceInput *activeVideoInput;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;

@end

@implementation ViewController 

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)StartCapture:(id)sender
{
if ([sender isSelected])
{
    [sender setSelected:NO];
    [self.captureOutput stopRecording];

}
else
{
[sender setSelected:YES];

if (!self.captureOutput)
    {
    self.captureOutput = [[AVCaptureMovieFileOutput alloc] init];
    [self.captureSession addOutput:self.captureOutput];
}
    [self.captureSession startRunning];
}
}

- (void)setCaptureSession
{
self.captureSession = [[AVCaptureSession alloc] init];
NSError *error;

// Set up hardware devices
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (videoDevice)
{
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
    if (input)
    {
        [self.captureSession addInput:input];
        self.activeVideoInput = input;
    }
}
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
if (audioDevice) {
    AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error];
    if (audioInput) {
        [self.captureSession addInput:audioInput];
    }
}

// Start running session so preview is available
[self.captureSession startRunning];

// Set up preview layer
dispatch_async(dispatch_get_main_queue(), ^{
    self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
    self.previewLayer.frame = self.previewView.bounds;
    self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

    //[[self.previewLayer connection] setVideoOrientation:[self currentVideoOrientation]];
    [self.previewView.layer addSublayer:self.previewLayer];
});
}

// Re-enable capture session if not running currently 
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (![self.captureSession isRunning])
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self.captureSession startRunning];
    });
}
}

// Stop running capture session when this view disappears
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if ([self.captureSession isRunning])
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self.captureSession stopRunning];
    });
}
}
@end

请告诉我应该在 IBAction 方法中编写什么(按下捕获视频按钮时调用该方法)以及如何使用 AVCaptureVideoPreviewLayer 使捕获的视频显示在屏幕上。

谢谢。

最佳答案

我找到了我丢失的东西。我这样做实在是太愚蠢了。我根本没有调用函数setCapture。因此屏幕上看不到任何东西。

我刚刚在 viewDidLoad 函数中调用了 setCapture 并解决了问题。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self setCaptureSession];
}

当手机方向改变时,视频仍然存在问题,我必须解决这个问题,但现在相机捕获的任何内容都可以在屏幕上看到。

关于iphone - 如何将 iPhone 摄像头拍摄的视频显示在屏幕上而不保存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18468964/

相关文章:

iphone - 使用 MPMoviePlayerController 播放视频,同时使用 AudioQueue 进行录制

android - 适用于Android设备的视频库

iphone - 每次我尝试通过存档验证我的应用程序时,Xcode 都会不断给我报亭错误

iphone - Mac 在 MonoTouch 和 XCode 中开发的系统要求

iphone - 清除 iPhone GPS 坐标缓存以测试较差的 GPS 条件?

ios - 更改 UILabel 属性字符串

jquery - 通过 jQuery 附加时,视频自动播放在 chrome 和 IE 上不起作用

html - 背景 div 中的视频无法在 IE 上运行

ios - 如何使用变体播放列表在视频播放器上切换视频质量

ios - 将正确定位的图层添加到上下文