AVCamSwift前置摄像头垂直翻转保存图像

时间:2015-12-18 22:09:52

标签: ios swift

我使用AVCamSwift拍照。但是,当我从内部相机拍摄照片时,它会将图像保存为垂直反转。代码的所有者确认这是一个错误。这些是我使用的代码:

@IBAction func snapStillImage(sender: AnyObject) {

    print("snapStillImage")
    dispatch_async(self.sessionQueue, {
        // Update the orientation on the still image output video connection before capturing.

        let videoOrientation =  (self.previewView.layer as! AVCaptureVideoPreviewLayer).connection.videoOrientation

        self.stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo).videoOrientation = videoOrientation

        // Flash set to Auto for Still Capture
        takePhotoScreen.setFlashMode(AVCaptureFlashMode.Auto, device: self.videoDeviceInput!.device)



        self.stillImageOutput!.captureStillImageAsynchronouslyFromConnection(self.stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo), completionHandler: {
            (imageDataSampleBuffer: CMSampleBuffer!, error: NSError!) in

            if error == nil {
                let data:NSData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
                let image:UIImage = UIImage( data: data)!

                let libaray:ALAssetsLibrary = ALAssetsLibrary()
                let orientation: ALAssetOrientation = ALAssetOrientation(rawValue: image.imageOrientation.rawValue)!
                libaray.writeImageToSavedPhotosAlbum(image.CGImage, orientation: orientation, completionBlock: nil)

                print("save to album")


            }else{
             print("Did not capture still image")
             print(error)
            }


        })


    })

}

@IBAction func changeCamera(sender: AnyObject) {


    print("change camera")

    self.cameraButton.enabled = false

    self.snapButton.enabled = false

    dispatch_async(self.sessionQueue, {

        let currentVideoDevice:AVCaptureDevice = self.videoDeviceInput!.device
        let currentPosition: AVCaptureDevicePosition = currentVideoDevice.position
        var preferredPosition: AVCaptureDevicePosition = AVCaptureDevicePosition.Unspecified

        switch currentPosition{
        case AVCaptureDevicePosition.Front:
            preferredPosition = AVCaptureDevicePosition.Back
        case AVCaptureDevicePosition.Back:
            preferredPosition = AVCaptureDevicePosition.Front
        case AVCaptureDevicePosition.Unspecified:
            preferredPosition = AVCaptureDevicePosition.Back

        }


        let device:AVCaptureDevice = takePhotoScreen.deviceWithMediaType(AVMediaTypeVideo, preferringPosition: preferredPosition)

        var videoDeviceInput: AVCaptureDeviceInput?

        do {
            videoDeviceInput = try AVCaptureDeviceInput(device: device)
        } catch _ as NSError {
            videoDeviceInput = nil
        } catch {
            fatalError()
        }

        self.session!.beginConfiguration()

        self.session!.removeInput(self.videoDeviceInput)

        if self.session!.canAddInput(videoDeviceInput){

            NSNotificationCenter.defaultCenter().removeObserver(self, name:AVCaptureDeviceSubjectAreaDidChangeNotification, object:currentVideoDevice)

            takePhotoScreen.setFlashMode(AVCaptureFlashMode.Auto, device: device)

            NSNotificationCenter.defaultCenter().addObserver(self, selector: "subjectAreaDidChange:", name: AVCaptureDeviceSubjectAreaDidChangeNotification, object: device)

            self.session!.addInput(videoDeviceInput)
            self.videoDeviceInput = videoDeviceInput

        }else{
            self.session!.addInput(self.videoDeviceInput)
        }

        self.session!.commitConfiguration()



        dispatch_async(dispatch_get_main_queue(), {
    //
            self.snapButton.enabled = true
            self.cameraButton.enabled = true
        })

    })

}

我尝试使用以下代码找到解决方案,但在前置凸轮中失败了。您有建议找到解决方法吗?

let reversedImage = UIImage(CGImage: pickedPhoto!.CGImage!, scale: 1.0, orientation: .LeftMirrored)

1 个答案:

答案 0 :(得分:0)

我用以下解决方案解决了这个问题:

let currentVideoDevice:AVCaptureDevice = self.videoDeviceInput!.device
let currentPosition: AVCaptureDevicePosition = currentVideoDevice.position              
if currentPosition == AVCaptureDevicePosition.Front

  {let reversedImage = UIImage(CGImage: image.CGImage!, scale: 1.0, orientation: .LeftMirrored)
sp.pickedPhoto = reversedImage}