2

私は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を使ってキャプチャ中の動画を画面に表示させる方法を教えてください。

ありがとう。

4

2 に答える 2

3

欠けていたものを見つけました。私がこれをするのは本当に愚かでした。私は関数をまったく呼び出しませんでしたsetCapture。したがって、画面には何も表示されませんでした。

viewDidLoad 関数を呼び出しsetCaptureて問題を解決しました。

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

電話の向きが変更されたときのビデオにはまだ問題がありますが、これに取り組む必要がありますが、カメラでキャプチャされているものはすべて画面に表示されます。

于 2013-08-28T07:15:32.267 に答える