如何只截取屏幕的一部分 [英] How to screenshot just part of a screen
问题描述
我需要将图像裁剪为高度为 25 且宽度为屏幕长度的矩形.
I need to crop an image to a rectangle with a height of 25 and a width the length of the screen.
我已准备好图像并将其放置在屏幕中央.我只想截取矩形.
I have the image ready and positioned in the center of the screen. I'd like to screenshot ONLY the rectangle.
我关注了这个答案,但它似乎不起作用出于某种原因对我来说.
I followed this answer, but it does not seem to be working for me for some reason.
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.view.frame.width,25), false, 0)
self.view.drawViewHierarchyInRect(CGRectMake(self.view.frame.width,25,view.bounds.size.width,view.bounds.size.height), afterScreenUpdates: true)
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
statusImage = image
推荐答案
您使用了错误的坐标.使用您的代码,绘图从视图的右上角开始,并在其右侧绘制空白空间.您应该使用 0 作为 X 值和负 Y 值.例如,如果你想截取一个从 Y=50 开始的条形图,你应该使用:
You are using wrong coordinates. With your code, the drawing starts at the top right corner of the view and draws void space at the right side of it. You should use 0 as X value and a negative Y value. For example, if you want to screenshot a bar starting at Y=50, you should use:
self.view.drawViewHierarchyInRect(CGRectMake(0, -50, view.bounds.size.width, view.bounds.size.height), afterScreenUpdates: true)
如果你想从视图的顶部绘制,只需将 0 作为 Y 值.
If you want to draw from the top of the view, just put 0 as Y value as well.
这篇关于如何只截取屏幕的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!