Ios 摄影机视图与结果图像不同
我正在制作一款iPad应用程序,它可以拍摄照片,并将照片与用户选择的相框混合在一起。我的问题是,相机视图显示的图像更多,它将在帧上使用,因为它将被剪切和调整大小,以适应帧 我试图将相机视图设置为相框内照片的大小,但相框分辨率太高,比iPad屏幕大 我也在调整图片以适应相框,如果我不这样做,图像就会缩小。所以我想做的是让相机只显示不会被裁剪的部分 这是我的裁剪方法:Ios 摄影机视图与结果图像不同,ios,swift,Ios,Swift,我正在制作一款iPad应用程序,它可以拍摄照片,并将照片与用户选择的相框混合在一起。我的问题是,相机视图显示的图像更多,它将在帧上使用,因为它将被剪切和调整大小,以适应帧 我试图将相机视图设置为相框内照片的大小,但相框分辨率太高,比iPad屏幕大 我也在调整图片以适应相框,如果我不这样做,图像就会缩小。所以我想做的是让相机只显示不会被裁剪的部分 这是我的裁剪方法: func crop(image: UIImage, width: Double, height: Double) -> UII
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
}
}
}
}
这实际上是一个愚蠢的错误,我传递了错误大小的原始图像。函数中的所有内容都正常工作。实际上,我传递了错误大小的原始图像,这是一个愚蠢的错误。功能中的一切都正常工作