相机视图与结果图像不同

时间:2019-05-29 15:04:32

标签: ios swift

我正在制作一个iPad应用程序,该应用程序可以拍照并与用户选择的框架融合在一起。我的问题是,相机视图会显示更多将在框架上使用的图像,因为它将被裁剪并调整大小以适合框架。

我试图将相机视图设置为框架内照片的大小,但框架分辨率过高且大于iPad屏幕。

我也在处理图片以适合框架,如果不这样做,图像将缩小。所以我想做的是使相机仅显示不会裁剪的内容。

这是我的裁剪方法:

func crop(image: UIImage, width: Double, height: Double) -> UIImage {

        let cgimage = image.cgImage!
        let contextImage: UIImage = UIImage(cgImage: cgimage)
        let contextSize: CGSize = contextImage.size
        var posX: CGFloat = 0.0
        var posY: CGFloat = 0.0
        var cgwidth: CGFloat = CGFloat(width)
        var cgheight: CGFloat = CGFloat(height)

        // See what size is longer and create the center off of that
        if contextSize.width > contextSize.height {
            posX = ((contextSize.width - contextSize.height) / 2)
            posY = 0
            cgwidth = contextSize.height
            cgheight = contextSize.height
        } else {
            posX = 0
            posY = ((contextSize.height - contextSize.width) / 2)
            cgwidth = contextSize.width
            cgheight = contextSize.width
        }

        let rect: CGRect = CGRect(x: posX, y: posY, width: cgwidth, height: cgheight)

        // Create bitmap image from context using the rect
        let imageRef: CGImage = cgimage.cropping(to: rect)!

        // Create a new image based on the imageRef and rotate back to the original orientation
        let image: UIImage = UIImage(cgImage: imageRef, scale: image.scale, orientation: image.imageOrientation)

        return image
    }

这是我设置相机的方式,prviewView是相机视图:

  guard let frontCamera = AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .front)
            else {
                fatalError("Front camera not found")
        }
        var error: NSError?
        var input: AVCaptureDeviceInput!
        do{
            input = try AVCaptureDeviceInput(device: frontCamera)
        }catch let error1 as NSError{
            error = error1
            input = nil
            print(error!.localizedDescription)
        }
        stillImageOutput = AVCapturePhotoOutput()
        if error == nil && session!.canAddInput(input) {
            session!.addInput(input)
            if session!.canAddOutput(stillImageOutput!){
                session!.addOutput(stillImageOutput!)                
                videoPreviewLayer = AVCaptureVideoPreviewLayer(session: session!)
                videoPreviewLayer!.videoGravity = .resizeAspect
                var videoOrientation = AVCaptureVideoOrientation(rawValue: 1)
                switch deviceOrientation {
                case .portrait:
                    videoOrientation = .portrait
                    break
                case .portraitUpsideDown:
                    videoOrientation = .portraitUpsideDown
                    break
                case .landscapeRight:
                    videoOrientation = .landscapeLeft
                    break
                case .landscapeLeft:
                    videoOrientation = .landscapeRight
                    break
                default:
                    break
                }
                videoPreviewLayer!.connection?.videoOrientation = videoOrientation ?? .portrait
                previewView.layer.addSublayer(videoPreviewLayer!)
                DispatchQueue.global(qos: .userInitiated).async { 
                    self.session!.startRunning()
                    DispatchQueue.main.async {
                        self.videoPreviewLayer!.frame = self.previewView.bounds
                    }
                }                
            }

        }

1 个答案:

答案 0 :(得分:0)

这实际上是一个愚蠢的错误,我通过了错误的原始图像尺寸。该功能一切正常。