2012年2月8日

【iPhone】Videoを使ってみる

今回はObjective-Cの解説です。

iPhoneのコーディングは久々なので、
Log.dとか書いちゃいますが、すぐ消します。

効率半減ですね。
って訳で今回はiPhoneのカメラを使ってみましょう。


まぁ、シングルViewAppで十分だと思います。

適当にちゃっちゃとコードだけ載せてきますね。
どうせコピペだろ?な?

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    _viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:_viewController];

    [self.window addSubview:navController.view];
    [self.window makeKeyAndVisible];
    return YES;
}


ViewController.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController : UIViewController {
  AVCaptureSession *captureSession;
}

@end


ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
  [super viewDidLoad];

  self.title = @"Record View"

  // create CaptureSession object
  captureSession = [[AVCaptureSession alloc]init];

  // set sessionPresent to Low
  if ([captureSession canSetSessionPreset:AVCaptureSessionPresetLow]) {
    captureSession.sessionPreset = AVCaptureSessionPresetLow;
  }
    
  // device setting
  NSError *error = nil;
  AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
  [captureSession addInput:input];
    
  // preview settings
  AVCaptureVideoPreviewLayer *preview = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
  preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
  preview.frame = self.view.bounds;
  [self.view.layer addSublayer:preview];

  // start capture session
  [captureSession startRunning];
}

- (void)viewDidUnload
{
  [super viewDidUnload];
  // Release any retained subviews of the main view.
  [captureSession stopRunning];

  captureSession = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
  return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

こんな感じで作ると
みたいな感じで出来るよ〜