2012-01-30 66 views
2

我試圖從正面模仿相機的應用過渡到後置攝像頭。它採用了UIViewAnimationOptionTransitionFlipFromLeft ..的iOS相機應用程序,閃爍時,相機背面

我目前的方法是在拍攝照片使用前置攝像頭,切換到後置攝像頭,使用後置攝像頭拍攝圖片,將它們加載到兩個UIImageView中,並執行圖像視圖的轉換..(隱藏當前攝像機流程)。

問題是當我打電話從前置攝像頭切換到後置攝像頭時,cameraView(AVCapturePreviewLayer)會立即切換視圖,然後我可以隱藏它(這發生在flipCamera中ToFront在最後一行.. [_captureSession commitConfiguration])...

希望這是有道理的,繼承人一些代碼我省略了一些,如果你認爲它會幫助讓我知道,我會張貼其餘的它。
任何幫助將不勝感激,謝謝

//class:RecordViewController 
//iVars for this class.. 
RecordVideoHandler *_recordHandler; 
UIImage *_firstImageForFlip; 
UIImage *_secondImageForFlip; 
UIImageView *_firstImageViewForFlip; 
BOOL isUsingFrontInput; 
AVCaptureVideoPreviewLayer *_capturePreviewLayer; 
UIView *_capturePreviewView; 

- (void)doCameraFlip:(UIButton *)sender 
{ 
    [_recordHandler addStillImageOutput]; 
    //This method calls imageReturned:(UIImage *)inImage; 
    [_recordHandler captureAndReturnStillImage]; 


} 
- (void)imageReturned:(UIImage *)inImage{ 

    if(_firstImageForFlip == nil){ 
     [_capturePreviewLayer setFrame:CGRectZero]; 
     _firstImageForFlip = inImage; 
     _firstImageViewForFlip = [[UIImageView alloc] initWithImage:_firstImageForFlip]; 


     if(isUsingFrontInput) 
      [_firstImageViewForFlip setTransform:CGAffineTransformMakeScale(-1.0, 1.0)]; 


     [_firstImageViewForFlip setFrame:_capturePreviewView.bounds]; 
     [_capturePreviewView addSubview:_firstImageViewForFlip]; 


     if(isUsingFrontInput){ 
      [_recordHandler flipCameraToBack]; 
     }else{ 
      [_recordHandler flipCameraToFront]; 
     } 
     [_recordHandler captureAndReturnStillImage]; 
    }else{ 
     _secondImageForFlip = inImage; 
     [_recordHandler removeStillImageOutput]; 
     [self finishCameraFlip]; 
    } 
} 
- (void)finishCameraFlip{ 
    UIImageView *secondImageView = [[UIImageView alloc] initWithImage:_secondImageForFlip]; 
    [secondImageView setFrame:_capturePreviewView.bounds]; 

    if(!isUsingFrontInput) 
     [secondImageView setTransform:CGAffineTransformMakeScale(-1.0, 1.0)]; 

    [UIView transitionWithView:_capturePreviewView duration:3.3f 
options:UIViewAnimationOptionTransitionFlipFromLeft animations: 
    ^{ 
     [_firstImageViewForFlip removeFromSuperview]; 
     [_capturePreviewLayer setFrame:_capturePreviewView.bounds]; 
     [_capturePreviewView addSubview:secondImageView]; 

    } completion: 
    ^(BOOL finished){ 
     [secondImageView removeFromSuperview]; 
    }]; 

    isUsingFrontInput = isUsingFrontInput ? NO : YES; 

    _firstImageForFlip = nil; 
    _secondImageForFlip = nil; 
    } 

類:RecordVideoHandler

//iVars for this class..  
    AVCaptureSession *_captureSession; 

    - (void)flipCameraToFront 
    { 
    [_captureSession beginConfiguration]; 
    for (AVCaptureInput *input in _captureSession.inputs) { 
     if (input == _captureRearInput) { 
      [_captureSession removeInput:_captureRearInput]; 
      [_captureSession addInput:_captureFrontInput]; 
     } 
    } 
    _currentCaptureDevice = _captureDeviceFrontFacing; 
    [_captureSession commitConfiguration]; 
} 

回答

6

如果有人想知道,我想它了。我必須使用performSelectors來等待下一個運行循環,因爲相機立即翻轉,而視圖的東西在運行循環之後等待......不確定具體情況,繼承代碼。

- (void)doCameraFlip:(UIButton *)sender 
{ 
    NSLog(@"doCameraFlip"); 
    [_recordHandler addStillImageOutput];  
    [_recordHandler captureAndReturnStillImage];  
} 
- (void)imageReturned:(UIImage *)inImage{ 
    [_recordHandler removeStillImageOutput];    
    _imageViewForFlip= [[UIImageView alloc] initWithImage:inImage]; 
    [_imageViewForFlip setFrame:_containerView.bounds]; 
    [_containerView addSubview:_imageViewForFlip]; 


    [self performSelector:@selector(cfe) withObject:nil afterDelay:0.0f]; 
    [self performSelector:@selector(cfe2) withObject:nil afterDelay:0.0f]; 

} 
- (void)cfe{ 
    if(isUsingFrontInput){ 
     [_recordHandler flipCameraToBack]; 
    }else{ 
     [_recordHandler flipCameraToFront]; 
    } 

} 
- (void)cfe2{ 
    [UIView transitionWithView:_containerView duration:3.0f options:UIViewAnimationOptionTransitionFlipFromRight animations:^{ 
     [_imageViewForFlip removeFromSuperview]; 
     [_containerView addSubview:_capturePreviewView]; 
    } completion:nil]; 
} 
+0

工作很好!非常感謝 – user1181046 2013-08-29 23:16:47