gpt4 book ai didi

ios - 如何自定义相机 View 并捕获叠加中的部分?

转载 作者:行者123 更新时间:2023-11-30 12:02:19 26 4
gpt4 key购买 nike

我需要构建一个与此类似的自定义相机 View :example image

我使用了 AVFoundation 并将 UIImageView 放置在 AVCaptureVideoPreviewLayer 上,它看起来几乎相同(尽管我不确定这是否是正确的方法,这就是我写标题的方式)。我正在捕获图像并将其保存到图库中,但我只需要中间矩形中的图像。

有什么建议吗?

提前致谢!

最佳答案

实际上 Nishant Bhindi 的答案需要一些更正。下面的代码将完成这项工作:

func cropToBounds(image: UIImage) -> UIImage
{
let contextImage: UIImage = UIImage(cgImage: image.cgImage!)
let contextSize: CGSize = contextImage.size
let widthRatio = contextSize.height/UIScreen.main.bounds.size.height
let heightRatio = contextSize.width/UIScreen.main.bounds.size.width

let width = (self.imgOverlay?.frame.size.width)!*widthRatio
let height = (self.imgOverlay?.frame.size.height)!*heightRatio
let x = (contextSize.width/2) - width/2
let y = (contextSize.height/2) - height/2
let rect = CGRect(x: x, y: y, width: width, height: height)

let imageRef: CGImage = contextImage.cgImage!.cropping(to: rect)!
let image: UIImage = UIImage(cgImage: imageRef, scale: 0, orientation: image.imageOrientation)
return image
}

关于ios - 如何自定义相机 View 并捕获叠加中的部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47053204/

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