Swift:裁剪没有TabBar和NavigationBar的屏幕截图 [英] Swift: Cropping a Screenshot without TabBar and NavigationBar

查看:93
本文介绍了Swift:裁剪没有TabBar和NavigationBar的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用以下命令生成的整个屏幕的屏幕截图screenshot:

I have a screenshot of the entire screen, screenshot, generated using the following:

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

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

我想要裁剪它,以便不包括选项卡栏,我尝试使用以下代码:

I'd like to crop it so that the tab bar is not included, and I tried using the following code:

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)开始,向右延伸小于屏幕宽度的一半,然后向下移动不到屏幕高度的一半.不过,我希望它包括 entire 屏幕,但选项卡栏所占的区域除外.有没有种这样的方法?

This code results in image showing just a small portion of the screen, a rectangle starting at the upper-left corner of the screen (0, 0), and extending right for less than half the width of the screen, then downward for less than half the height of the screen. I'd like, though, for it to include the entire screen, except for the region occupied by the tab bar. Is there a such a way to crop it?

推荐答案

据此

新图像由
创建 1)通过调用CGRectIntegral;
rect调整为整数范围 2)将结果与原点(0,0)和大小的矩形相交 等于image的大小;
3)引用生成的矩形内的像素, 图片数据的第一个像素作为图片的原点.
如果结果矩形为空矩形,则此函数返回 空.

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.

如果W和H分别是图像的宽度和高度,则 点(0,0)对应于图像数据的第一个像素;重点 (W-1,0)是图像数据第一行的最后一个像素; (0,H-1) 是图像数据最后一行的第一个像素;并且(W-1,H-1)是 图片数据最后一行的最后一个像素.

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

You will need to have a crop function like this. You may need adjust the calculation of the 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
}

这篇关于Swift:裁剪没有TabBar和NavigationBar的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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