我用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)

最佳答案

我通过以下解决方案解决了该错误:

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}

关于ios - AVCamSwift前置摄像头可垂直反转保存图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34364688/

10-09 15:34