1

ユーザーからの入力なしで写真を撮るには、iOS カメラが必要です。どうすればこれを行うことができますか?これまでの私のコードは次のとおりです。

-(void)initCapture{
    AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] init];

    AVCaptureStillImageOutput *newStillImageOutput = [[AVCaptureStillImageOutput alloc] init];

    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
                                AVVideoCodecJPEG, AVVideoCodecKey,
                                nil];


    [newStillImageOutput setOutputSettings:outputSettings];


    AVCaptureSession *newCaptureSession = [[AVCaptureSession alloc] init];

    if ([newCaptureSession canAddInput:newVideoInput]) {
        [newCaptureSession addInput:newVideoInput];
    }
    if ([newCaptureSession canAddOutput:newStillImageOutput]) {
        [newCaptureSession addOutput:newStillImageOutput];
    }
    self.stillImageOutput = newStillImageOutput;
}

他に何を追加する必要があり、ここからどこに行けばよいですか? 動画は撮りたくありません。静止画を 1 枚だけ撮ります。また、後で画像を UIImage に変換するにはどうすればよいですか? ありがとう

4

3 に答える 3

1

AVCaptureStillImageOutput がありstillImageOutputます。さらに、後でアクセスできる場所にキャプチャ セッションを保存しておく必要があります。それを呼び出しますself.sess。それで:

AVCaptureConnection *vc = 
    [self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo];
[self.sess startRunning];
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:vc
                                          completionHandler:
 ^(CMSampleBufferRef buf, NSError *err) {
     NSData* data = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:buf];
     UIImage* im = [UIImage imageWithData:data];
     dispatch_async(dispatch_get_main_queue(), ^{
         UIImageView* iv = [[UIImageView alloc] initWithFrame:someframe];
         iv.contentMode = UIViewContentModeScaleAspectFit;
         iv.image = im;
         [self.view addSubview: iv];
         [self.iv removeFromSuperview]; // in case we already did this
         self.iv = iv;
         [self.sess stopRunning];
     });
 }];
于 2014-03-15T22:08:54.593 に答える
1

https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVCaptureSession_Class/Reference/Reference.html

ユーザー入力なしで写真を撮る方法については、AVCaptureSession を参照してください。カメラ用の AVCaptureDevice をセッションに追加する必要があります。

于 2014-03-15T16:46:48.310 に答える
0

@Salil が言うように、アップルの公式 AVFoundation は非常に便利です。それを見て、主な概念を理解してください。その後、サンプル コードをダウンロードして、目標を達成する方法を確認できます。

プレビュー レイヤーを使用して静止画を撮影するサンプル コードを次に示します。

于 2014-06-18T15:21:26.973 に答える