gpt4 book ai didi

ios - iOS 相机拍摄的调整大小的图像在拍摄后显示不一样

转载 作者:行者123 更新时间:2023-11-30 13:36:10 24 4
gpt4 key购买 nike

我的问题是,图像拍摄后,它会自行缩放,因此它看起来与我通过查看相机 View 所期望的 View 并不完全相同。

为了处理相机工作,我使用开源库,一个名为 CameraManager 的 pod。您可以在下面找到我的代码,稍后我将解释图像如何自行缩放。

import UIKit
import CameraManager

class ViewController: UIViewController {
@IBOutlet weak var height: NSLayoutConstraint!
@IBOutlet weak var width: NSLayoutConstraint!
@IBOutlet weak var cameraView: UIView!

@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var imageHeight: NSLayoutConstraint!
@IBOutlet weak var imageWidth: NSLayoutConstraint!

let cameraManager = CameraManager()

override func viewDidLoad() {
super.viewDidLoad()

imageView.hidden = true

height.constant = self.view.frame.width
width.constant = self.view.frame.width

imageHeight.constant = self.view.frame.width
imageWidth.constant = self.view.frame.width

cameraManager.addPreviewLayerToView(self.cameraView)
cameraManager.writeFilesToPhoneLibrary = false
}

@IBAction func shootImage(sender: AnyObject) {
print("bastim")
print("self.imageView.frame.height \(self.cameraView.frame.height) and self.imageView.frame.width \(self.cameraView.frame.width)")
print("camera x \(self.cameraView.frame.origin.x) and camera y \(self.cameraView.frame.origin.y)")

cameraManager.capturePictureWithCompletition({ (image, error) -> Void in
var ratio: CGFloat
var delta: CGFloat
var offset: CGPoint

let size: CGSize = CGSizeMake(self.cameraView.frame.width, self.cameraView.frame.height)

if(image?.size.width > image?.size.height) {
ratio = self.cameraView.frame.width / image!.size.width
delta = ratio*image!.size.width - ratio*image!.size.height
offset = CGPointMake(delta/2, 0)
}else {
ratio = self.cameraView.frame.width / image!.size.height
delta = ratio*image!.size.height - ratio*image!.size.width
offset = CGPointMake(0, delta/2)
}
print("delta \(delta)")

let clipRect: CGRect = CGRectMake(-offset.x, -offset.y, (ratio * image!.size.width) + delta, (ratio * image!.size.height) + delta)

if (UIScreen.mainScreen().respondsToSelector(Selector("scale")) && UIScreen.mainScreen().scale >= 2.0) {
UIGraphicsBeginImageContextWithOptions(size, true, 0.0)
}else {
UIGraphicsBeginImageContext(size)
}

UIRectClip(clipRect)
image?.drawInRect(clipRect)
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

self.cameraView.hidden = true
self.imageView.hidden = false
self.imageView.image = newImage
})
}

@IBAction func repeatProcess(sender: AnyObject) {
self.cameraView.hidden = false
self.imageView.hidden = true
}

}

下面您可以详细找到我的问题。

它可能会缩放错误或裁剪错误的尺寸。我不确定两者中哪一个是正确的。说实话,我有点迷茫,找不到这里的问题。

最佳答案

终于得到答案了!有同样问题的 friend 可以引用下面的代码。这对我来说非常有用:

 @IBAction func shootImage(sender: AnyObject) {

print("self.cameraView.frame.height \(self.cameraView.frame.height) and self.cameraView.frame.width \(self.cameraView.frame.width)")
print("camera x \(self.cameraView.frame.origin.x) and camera y \(self.cameraView.frame.origin.y)")

print("self.imageView.frame.height \(self.imageView.frame.height) and self.imageView.frame.width \(self.imageView.frame.width)")
print("image view x \(self.imageView.frame.origin.x) and image view y \(self.imageView.frame.origin.y)")


cameraManager.capturePictureWithCompletition({ (image, error) -> Void in
let scaleTransform: CGAffineTransform!
let origin: CGPoint

if(image!.size.width > image!.size.height) {
let scaleRatio: CGFloat = self.cameraView.frame.height / image!.size.height
scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio)

origin = CGPointMake(-(image!.size.width - image!.size.height) / 2.0, 0)
}else {
let scaleRatio: CGFloat = self.cameraView.frame.width / image!.size.width
scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio)

origin = CGPointMake(0, -(image!.size.height - image!.size.width) / 2.0)
}

let size: CGSize = CGSizeMake(self.cameraView.frame.width, self.cameraView.frame.height)

if (UIScreen.mainScreen().respondsToSelector(Selector("scale")) && UIScreen.mainScreen().scale >= 2.0) {
UIGraphicsBeginImageContextWithOptions(size, true, 0.0)
}else {
UIGraphicsBeginImageContext(size)
}

let context = UIGraphicsGetCurrentContext()
CGContextConcatCTM(context, scaleTransform)

image!.drawAtPoint(origin)

let newImage = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

self.cameraView.hidden = true
self.imageView.hidden = false
self.imageView.image = newImage

print("newImage width \(newImage.size.width )")
print("newImage height \(newImage.size.height)")
})
}

注意:如果您不使用此库,请不要打扰cameraManager实例。相关代码只是简单地从镜头中获取图像,因此图像实例是 ios Camera 拍摄的图像,它是 AVFoundation 的输出。

旁白信息 - 不过,我建议您查看一下 CameraManager pod。它极大地简化了 AVFoundationUsage。

关于ios - iOS 相机拍摄的调整大小的图像在拍摄后显示不一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36049958/

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