首页 > 解决方案 > 与结果图像不同的相机视图

问题描述

我正在制作一个 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
                    }
                }                
            }

        }

标签: iosswift

解决方案


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


推荐阅读