扩展功能屏幕截图未在iPhone和iPad中捕获相同区域 [英] extension function screenshot not capturing the same area in iphone and ipad

查看:184
本文介绍了扩展功能屏幕截图未在iPhone和iPad中捕获相同区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用扩展功能来拍摄uiview的屏幕截图.问题在于结果看起来非常不同.我希望无论ipad还是iphone的天气如何,照片看起来都一样.我正在手动输入约束,所以我希望图像是相同的.我想使用该功能转到viewcontroller的原始位置或中心,并设置为200高度和200宽度.因此,无论使用哪种设备,图像都应该相同,因为它始于中央且尺寸相同.

I am using a extension function to take a screenshot of a uiview. The problem is that the results look very different. I want the photo to look the same regardless of weather its a ipad or iphone. I am manually entering the constraints so I expect the images to be the same. I want to using the function to go to the orgin or center of the viewcontroller and be 200 height and 200 width. So the image should be the same regardless of the device because it starts in the center and is the same size.

     let vex = self.view.screenshot(for: CGRect(x: 0, y:UIScreen.main.bounds.size.height*0.65,, width: 100, height: 100), with: UIImage(named: "a.png"))

    extension UIView {
/// Takes a screenshot of a UIView, with an option to clip to view bounds and place a waterwark image
/// - Parameter rect: offset and size of the screenshot to take
/// - Parameter clipToBounds: Bool to check where self.bounds and rect intersect and adjust size so there is no empty space
/// - Parameter watermark: UIImage of the watermark to place on top
func screenshot(for rect: CGRect, clipToBounds: Bool = true, with watermark: UIImage? = nil) -> UIImage {
    var imageRect = rect
    if clipToBounds {
        imageRect = bounds.intersection(rect)
    }
    return UIGraphicsImageRenderer(bounds: imageRect).image { _ in
    drawHierarchy(in: CGRect(origin: .zero, size: bounds.size), afterScreenUpdates: true)
    watermark?.draw(in: CGRect(x: 0, y: 0, width: 32, height: 32))
    }
}}

IPHONE

IPAD

推荐答案

看看下面的屏幕截图.

Take a look at the below screenshot. 


View hierarchy   
 -self.view
   -blueView (subview of self.view) 
   -redView   (subview of self.view)

完整屏幕截图

请注意,屏幕截图目标(基于X = 0和Y = screen.y乘数)根据设备大小有何不同?使用提供的扩展程序,您可以根据视图层次结构以几种不同的方式解决上述问题. 如果您的层次结构仍然如上所列,则可以执行以下操作:

Notice how the screenshot target (based on X = 0 and Y = screen.y multiplier) differs based on device size?


 With the supplied extension you can fix the above issue in several different ways depending on view hierarchy. In case your hierarchy remains as listed above you can do something like this:


选项1 如果要截取blueView内部内容的屏幕截图,则可以执行以下操作:(因为此blueView是IBOutlet或通过编程设置的属性)

Option 1 If you want to take a screenshot of exactly what’s inside of the blueView then you can do this:
(for this blueView is either an IBOutlet or a property that is set programmatically)

let image = self.view.screenshot(for: blueView.frame, clipToBounds: true, with: UIImage(named: "star))

上面为self.view的所有子视图在给定的rect上截取了self.view的屏幕截图. (下面的输出)

The above takes a screenshot of self.view at the given rect for all of subviews of self.view. (output below)

选项2 如果您的视图层次结构更改为以下内容: -self.view -blueView(self.view的子视图) -redView(blueView的子视图)

Option 2 If your view hierarchy changes to something like this: -self.view - blueView (subview of self.view) - redView (subview of blueView)


并且您只想拍摄blueView和子视图的快照,则可以通过执行以下操作来简化上述操作:

And you want to take a snapshot of just blueView and subviews, then you can simplify the above by doing:

let image = blueView.screenshot(for: blueView.bounds, with: UIImage(named: "star)) 之所以可行,是因为redView是blueView的子视图(与上面的屏幕截图相同)

let image = blueView.screenshot(for: blueView.bounds, with: UIImage(named: "star")) This works, because redView is subview of blueView (output same as the screenshot above)

选项3 现在假设您要在屏幕中央拍摄一个self.view的屏幕截图,屏幕截图大小为300,然后您可以执行以下操作

Option 3 Now lets say you want to take a screenshot of self.view in the center with a screenshot size of 300 then you can do something like this

let size = CGSize(width: 300, height: 300) // notice this is bigger than blueView size
let rect = CGRect(origin: CGPoint(x: self.view.center.x - size.width / 2, y: self.view.center.y - size.height / 2), size: size)
let image = self.view.screenshot(for: rect, clipToBounds: false, with: UIImage(named: "star"))

所有这些都可以优化吗?是的,但我正在尝试逐步为您说明问题. (以下是此输出)

Can all of these be optimized? YES, but I’m trying to spell things out for you step by step. (below is the output of this)

顺便说一句,这些屏幕截图中的星星是水印

对于那些想知道的人,OP的扩展名来自于此 answer ,并且自此之时起进行了一些更新.问题.

For those wondering, the OP's extension is from this answer and it has been updated a little from the time of this question.

这篇关于扩展功能屏幕截图未在iPhone和iPad中捕获相同区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆