2017-12-26 16 views
0

私の必要条件は、UIImagePickerControllerをプレビューせずに画像をキャプチャすることです。次のコードを使用して、同じものを提示せずに画像をキャプチャしました。AVCapturePhotoOutputが適切な画像を返さない

- (void)clickImage 
{ 
    AVCaptureDevice *rearCamera = [self checkIfRearCameraAvailable]; 

    if (rearCamera != nil) 
    { 
     photoSession = [[AVCaptureSession alloc] init]; 

     NSError *error; 
     AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:rearCamera error:&error]; 

     if (!error && [photoSession canAddInput:input]) 
     { 
      [photoSession addInput:input]; 

      AVCapturePhotoOutput *output = [[AVCapturePhotoOutput alloc] init]; 


      if ([photoSession canAddOutput:output]) 
      { 
       [photoSession addOutput:output]; 

       AVCaptureConnection *videoConnection = nil; 

       for (AVCaptureConnection *connection in output.connections) 
       { 
        for (AVCaptureInputPort *port in [connection inputPorts]) 
        { 
         if ([[port mediaType] isEqual:AVMediaTypeVideo]) 
         { 
          videoConnection = connection; 
          break; 
         } 
        } 
        if (videoConnection) 
        { 
         break; 
        } 
       } 

       if (videoConnection) 
       { 
        [photoSession startRunning]; 

        [output capturePhotoWithSettings:[AVCapturePhotoSettings photoSettings] delegate:self]; 
       } 
      } 
     } 
    } 
} 

- (AVCaptureDevice *)checkIfRearCameraAvailable 
{ 
    AVCaptureDevice *rearCamera; 

    AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession = 
    [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera] 
                  mediaType:AVMediaTypeVideo 
                  position:AVCaptureDevicePositionBack]; 

    NSArray *allCameras = [captureDeviceDiscoverySession devices]; 


    for (int i = 0; i < allCameras.count; i++) 
    { 
     AVCaptureDevice *camera = [allCameras objectAtIndex:i]; 

     if (camera.position == AVCaptureDevicePositionBack) 
     { 
      rearCamera = camera; 
     } 
    } 

    return rearCamera; 
} 

- (void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhotoSampleBuffer:(CMSampleBufferRef)photoSampleBuffer previewPhotoSampleBuffer:(CMSampleBufferRef)previewPhotoSampleBuffer resolvedSettings:(AVCaptureResolvedPhotoSettings *)resolvedSettings bracketSettings:(AVCaptureBracketedStillImageSettings *)bracketSettings error:(NSError *)error 
{ 
    if (error) 
    { 
     NSLog(@"error : %@", error.localizedDescription); 
    } 

    if (photoSampleBuffer) 
    { 
     NSData *data = [AVCapturePhotoOutput JPEGPhotoDataRepresentationForJPEGSampleBuffer:photoSampleBuffer 
                    previewPhotoSampleBuffer:previewPhotoSampleBuffer]; 
     UIImage *image = [UIImage imageWithData:data]; 

     _imgView.image = image; 
    } 
} 

上記のコードを使用してイメージをキャプチャしましたが、出力イメージがナイトモードまたはネガティブイメージのように表示されています。

コードを確認してこれを修正してください。私はApple開発者サイトからサンプルコードを以下のを発見した

答えて

関連する問題