Swift:裁剪截图 [英] Swift: Cropping a Screenshot

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

问题描述

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

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)!

此代码导致图像仅显示屏幕的一小部分,一个矩形从屏幕的左上角开始(0,0),向右延伸不到屏幕宽度的一半,然后向下不到屏幕高度的一半。但是,我想要包含整个屏幕,除了标签栏占用的区域。有没有这种方法来裁剪它?

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)引用生成的矩形中的像素,将图像数据的
第一个像素视为图像的原点。

如果生成的矩形是空矩形,此函数返回
NULL。

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:裁剪截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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