【问题标题】:Capture screen based on UIImage size根据 UIImage 大小截屏
【发布时间】:2017-12-09 10:50:39
【问题描述】:

我在捕获自定义视图时遇到问题。我正在尝试设置大小为UIImage 图像数据大小的捕获图像,但是当我截取屏幕截图时,图像是这样的! :

图像缩小! 这是代码:

  UIGraphicsBeginImageContextWithOptions((self.photoImage.size), false, UIScreen.main.scale)

        self.captureView.layer.render(in: UIGraphicsGetCurrentContext()!)

        self.photoToShare = UIGraphicsGetImageFromCurrentImageContext()

        print(self.photoToShare.size) //the size is right 

        UIGraphicsEndImageContext()

        self.photo.image = self.photoToShare!

什么问题?!

我累了: self.captureView.drawHierarchy(in: self.captureView.bounds , afterScreenUpdates: true)

还是没有成功

编辑

我尝试了这个方法,捕获然后裁剪它,但仍然没有成功

UIGraphicsBeginImageContextWithOptions(self.captureView.bounds.size, false, UIScreen.main.scale)
            self.captureView.drawHierarchy(in: self.captureView.bounds, afterScreenUpdates: true)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()

            let cropRect = CGRect(x: 0, y: 0, width: (self.photoImage.size.width) , height: (self.photoImage.size.height))
            UIGraphicsBeginImageContextWithOptions(cropRect.size, false, UIScreen.main.scale)
            image?.draw(in: cropRect)
            let finalImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()

            self.photo.image = finalImage

但图像会失真:

【问题讨论】:

  • 尝试在UIGraphicsBeginContextWithOptions 之前调用[self.captureView setNeedsLayout]; [self.captureView layoutIfNeeded]。另外,如果这不起作用,请尝试使用self.captureView.bounds,而不是将大小设置为photoImage.size
  • 谢谢我没有工作!实际上我需要照片数据大小。
  • 您是否尝试过像stackoverflow.com/questions/11318020/… 那样缩放捕获的图像?
  • @Ellen 是的,但实际上我需要用准确的 UIimage 的数据大小宽度和高度来捕获 UIIView
  • 只是为了确定在 ImageView 中显示图像时您使用的是 AspectFit 比例还是比例填充?

标签: ios iphone swift xcode uiimage


【解决方案1】:

试试这个:

extension UIView {
    var screenCapture: UIImage {
        UIGraphicsBeginImageContextWithOptions(bounds.size, opaque, 0.0)
        drawViewHierarchyInRect(bounds, afterScreenUpdates: true)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}

将其放入 UImageView 中:

let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
imageView.image = someView.screenCapture
imageView.frame = someFrame
someSuperview.addSubview(imageView)

(对所有somes 感到抱歉,但希望它不会造成混淆 :)

希望这会有所帮助!

【讨论】:

  • @Mc.Lover,为什么尺寸必须是“图像数据尺寸”?另外,您是在设备上还是模拟器上进行测试?
【解决方案2】:

我尝试使用 aspectToFit 内容模式捕获 imageview 的屏幕截图,并且使用以下方法没有恒定的宽度和高度

func captureShot() {
        let layer : CALayer = self.imageView!.layer
        UIGraphicsBeginImageContextWithOptions((layer.frame.size), false, 1.0);
        layer.render(in: UIGraphicsGetCurrentContext()!)
        let screenshot = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

    }

& 返回的屏幕截图具有相同的图像尺寸 (229*220),与图像的原始尺寸相同。

【讨论】:

  • 谢谢!我试试
  • 非常感谢它运行良好!但我添加了一个裁剪功能来将 Image 保存为 image.image 大小。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-19
  • 2021-12-28
  • 2013-05-12
  • 2012-01-03
  • 2020-09-29
  • 2017-12-02
相关资源
最近更新 更多