gpt4 book ai didi

ios - 相机 View 与结果图像不同

转载 作者:行者123 更新时间:2023-11-28 07:26:50 25 4
gpt4 key购买 nike

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

我尝试将相机 View 设置为框架内照片的大小,但框架分辨率太高且大于 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 是相机 View :

  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
}
}
}

}

最佳答案

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

关于ios - 相机 View 与结果图像不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56363585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com