2014-09-05 116 views
2

我使用蘋果公司製造的AVCam作爲我的自定義相機視圖。老實說,如果你第一次看到它,那麼就不要簡單地理解課程AVCamViewController中發生了什麼。AVCam保存全屏捕獲的圖像

現在我很感興趣,他們如何設置捕獲圖像的幀。我試圖找到一些名人或類似的東西,但我還沒有找到任何東西。

我在谷歌搜索,發現這裏的答案AVCam not in fullscreen

但是,當我實施瞭解決方案,我才意識到,這只是做直播攝像頭預覽層具有相同的大小作爲我的觀點,但是當應用程序的方法保存圖像圖片中的- (IBAction)snapStillImage:(id)sender圖片仍然是左右兩條條紋。

我的問題是如何刪除此條紋或在源代碼中的哪一行蘋果設置這個東西?

此外,作爲附加的子問題,我如何設置類型創建照片,因爲應用程序要求我「麥克風設置」,我不需要它只是需要照片,就是這樣。

來自蘋果的這段代碼將圖片保存到照片庫。

- (IBAction)snapStillImage:(id)sender 
{ 
    dispatch_async([self sessionQueue], ^{ 
     // Update the orientation on the still image output video connection before capturing. 
     [[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] videoOrientation]]; 

     // Flash set to Auto for Still Capture 
     [AVCamViewController setFlashMode:AVCaptureFlashModeAuto forDevice:[[self videoDeviceInput] device]]; 

     // Capture a still image. 
     [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { 

      if (imageDataSampleBuffer) 
      { 
       NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; 

       UIImage *image = [[UIImage alloc] initWithData:imageData]; 

       UIImage *proccessingImage = [SAPAdjustImageHelper adjustImage:image]; 

       NSNumber *email_id = [SAPCoreDataEmailHelper currentEmailId]; 

       [SAPFileManagerHelper addImage:proccessingImage toFolderWithEmailId:email_id]; 
      } 
     }]; 
    }); 
} 
+0

請,根據添加代碼或截圖預覽... – hmdeep 2014-09-05 11:49:30

+0

我的經驗,它的做工精細的iPhone5的,但問題是,只在iPhone 4,右??? – hmdeep 2014-09-05 11:56:14

+0

您可以在此處下載代碼https://developer.apple.com/library/ios/samplecode/AVCam/Introduction/Intro.html,如上所述。我沒有修改任何 – 2014-09-05 11:57:26

回答

5

您是否在設置會話預設?

您可以使用會話預設中設置的會話AVCaptureSessionPresetPhoto

對於另一個子問題:您只需要添加AVCaptureStillImageOutput輸出。

如何設置會話預設?

[session setSessionPreset:AVCaptureSessionPresetPhoto]; 

如何配置會話只使用StillImageOutput拍照和?

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Create the AVCaptureSession 
    AVCaptureSession *session = [[AVCaptureSession alloc] init]; 
    [self setSession:session]; 

    // Setup the preview view 
    [[self previewView] setSession:session]; 

    // Setup the session Preset   
    [session setSessionPreset:AVCaptureSessionPresetPhoto]; 

    // Check for device authorization 
    [self checkDeviceAuthorizationStatus]; 

    dispatch_queue_t sessionQueue = dispatch_queue_create("session queue", DISPATCH_QUEUE_SERIAL); 
    [self setSessionQueue:sessionQueue]; 

    dispatch_async(sessionQueue, ^{ 
     //[self setBackgroundRecordingID:UIBackgroundTaskInvalid]; 

     NSError *error = nil; 

     AVCaptureDevice *videoDevice = [AVCamViewController deviceWithMediaType:AVMediaTypeVideo preferringPosition:AVCaptureDevicePositionBack]; 
     AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error]; 

     if (error) 
     { 
      NSLog(@"%@", error); 
     } 

     if ([session canAddInput:videoDeviceInput]) 
     { 
      [session addInput:videoDeviceInput]; 
      [self setVideoDeviceInput:videoDeviceInput]; 

      dispatch_async(dispatch_get_main_queue(), ^{ 
       // Why are we dispatching this to the main queue? 
       // Because AVCaptureVideoPreviewLayer is the backing layer for AVCamPreviewView and UIView can only be manipulated on main thread. 
       // Note: As an exception to the above rule, it is not necessary to serialize video orientation changes on the AVCaptureVideoPreviewLayer’s connection with other session manipulation. 

       [[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] setVideoOrientation:(AVCaptureVideoOrientation)[self interfaceOrientation]]; 
      }); 
     } 

AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; 
     if ([session canAddOutput:stillImageOutput]) 
     { 
      [stillImageOutput setOutputSettings:@{AVVideoCodecKey : AVVideoCodecJPEG}]; 
      [session addOutput:stillImageOutput]; 
      [self setStillImageOutput:stillImageOutput]; 
     } 
    }); 
} 
+0

好的我今天會檢查您的答案,非常感謝)我會將它標記爲已接受的問題,我測試它) – 2014-09-08 20:24:45

+0

由於某種原因,應用程序崩潰後,我採取2或一張照片 – 2014-09-08 21:39:43

+0

所以我剛剛添加了一行[會話setSessionPreset:AVCaptureSessionPresetPhoto];它工作完美,但當應用程序開始調用snapStillImage應用程序崩潰。刪除這行應用程序需要照片,但有黑色條紋 – 2014-09-08 21:50:17

0

需要的字段,以保持縱橫比,如果想避開他們,你應該改變videoGravityAVCaptureVideoPreviewLayer,或在UIImageViewcontentMode顯示所拍攝圖像的黑人。
這是一個預期的行爲,不理解你的關注。

+0

當我使用UIIMagePickerController時,我確實很棒!圖像沒有任何條紋。但是當運行蘋果源時,我得到了一半的圖像,而不是100的大小,這是一個問題,視頻重力是可以的,但只能用於預覽,任何情況下,圖庫中保存的圖像將具有iPad屏幕的一半大小, t使用任何UIImageView – 2014-09-05 12:55:15

+0

我已經添加了代碼,蘋果用於保存圖像到圖書館,是否有蘋果提供的AVCam示例中的任何UIImageView?關於您說的contentMode? – 2014-09-05 12:57:27

+0

我也發現這個鏈接http://stackoverflow.com/questions/20497575/avcam-not-in-fullscreen,但正如我所說,它只是讓全屏幕的現場錄音組件。使用這種方法將圖像保存到庫後 - (IBAction)snapStillImage:(id)sender圖像將具有iPad屏幕大小的一半大小。 – 2014-09-05 12:59:19