ios - Swift:在没有 TabBar 和 NavigationBar 的情况下裁剪屏幕截图

标签 ios swift screenshot cgimage uigraphicscontext

我有整个屏幕的屏幕截图,screenshot,使用以下内容生成:

let layer = UIApplication.sharedApplication().keyWindow!.layer
let scale = UIScreen.mainScreen().scale
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);

layer.renderInContext(UIGraphicsGetCurrentContext())
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

我想裁剪它以便不包括标签栏,我尝试使用以下代码:

let crop = CGRectMake(0, 0, //"start" at the upper-left corner
self.view.bounds.width, //include half the width of the whole screen
self.view.bounds.height + self.navigationController!.navigationBar.frame.height) //include the height of the navigationBar and the height of view

let cgImage = CGImageCreateWithImageInRect(screenshot.CGImage, crop)
let image: UIImage = UIImage(CGImage: cgImage)!

这段代码导致 image 只显示屏幕的一小部分,一个从屏幕左上角 (0, 0) 开始的矩形,向右延伸不到屏幕的一半屏幕宽度,然后向下移动不到屏幕高度的一半。不过,我希望它包括整个 屏幕,但标签栏占据的区域除外。有没有这样的方法来裁剪它?

最佳答案

据此

The new image is created by
1) adjusting rect to integral bounds by calling CGRectIntegral;
2) intersecting the result with a rectangle with origin (0, 0) and size equal to the size of image;
3) referencing the pixels within the resulting rectangle, treating the first pixel of the image data as the origin of the image.
If the resulting rectangle is the null rectangle, this function returns NULL.

If W and H are the width and height of image, respectively, then the point (0,0) corresponds to the first pixel of the image data; the point (W-1, 0) is the last pixel of the first row of the image data; (0, H-1) is the first pixel of the last row of the image data; and (W-1, H-1) is the last pixel of the last row of the image data.

您将需要像这样的裁剪功能。您可能需要调整 bottomBarHeight

的计算
func takeScreenshot(sender: AnyObject) {
    let layer = UIApplication.sharedApplication().keyWindow!.layer
    let scale = UIScreen.mainScreen().scale
    UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);

    layer.renderInContext(UIGraphicsGetCurrentContext())
    let screenshot = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    let croppedImage = self.cropImage(screenshot)
}

func cropImage(screenshot: UIImage) -> UIImage {
    let scale = screenshot.scale
    let imgSize = screenshot.size
    let screenHeight = UIScreen.mainScreen().applicationFrame.height
    let bound = self.view.bounds.height
    let navHeight = self.navigationController!.navigationBar.frame.height
    let bottomBarHeight = screenHeight - navHeight - bound
    let crop = CGRectMake(0, 0, //"start" at the upper-left corner
        (imgSize.width - 1) * scale, //include half the width of the whole screen
        (imgSize.height - bottomBarHeight - 1) * scale) //include the height of the navigationBar and the height of view

    let cgImage = CGImageCreateWithImageInRect(screenshot.CGImage, crop)
    let image: UIImage = UIImage(CGImage: cgImage)!
    return image
}

关于ios - Swift:在没有 TabBar 和 NavigationBar 的情况下裁剪屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32021204/

相关文章:

ios - Swift - 使用 'as! Dictionary<String, AnyObject>' 时为零

swift - 将 Ensembles 添加到现有 Coredata/iCloud App 的步骤

delphi - 如何使用FireMonkey截图(多平台)

java - 在 selenium 中使用 ashot 的滚动屏幕截图不适用于 Internet Explorer 11 版本,但在 Chrome 和 Firefox 中工作正常

swift - 将一个协议(protocol)转换为另一个协议(protocol) Swift

c++ - 使用 Qt5 截取完整桌面的屏幕截图

ios - 因 XCode 8 方向的特征而异

objective-c - 应用程序重新打开时执行代码

ios - 如何添加圆到矩形? UIBezierPath

ios - UITableViewCell 中的 UIStepper 在点击时出现问题