ios – Swift:裁剪截图

前端之家收集整理的这篇文章主要介绍了ios – Swift:裁剪截图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有整个屏幕截图,截图,使用以下内容生成
  1. let layer = UIApplication.sharedApplication().keyWindow!.layer
  2. let scale = UIScreen.mainScreen().scale
  3. UIGraphicsBeginImageContextWithOptions(layer.frame.size,false,scale);
  4.  
  5. layer.renderInContext(UIGraphicsGetCurrentContext())
  6. let screenshot = UIGraphicsGetImageFromCurrentImageContext()
  7. UIGraphicsEndImageContext()

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

  1. let crop = CGRectMake(0,//"start" at the upper-left corner
  2. self.view.bounds.width,//include half the width of the whole screen
  3. self.view.bounds.height + self.navigationController!.navigationBar.frame.height) //include the height of the navigationBar and the height of view
  4.  
  5. let cgImage = CGImageCreateWithImageInRect(screenshot.CGImage,crop)
  6. let image: UIImage = UIImage(CGImage: cgImage)!

代码导致图像只显示屏幕的一小部分,一个矩形从屏幕的左上角开始(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的计算

  1. func takeScreenshot(sender: AnyObject) {
  2. let layer = UIApplication.sharedApplication().keyWindow!.layer
  3. let scale = UIScreen.mainScreen().scale
  4. UIGraphicsBeginImageContextWithOptions(layer.frame.size,scale);
  5.  
  6. layer.renderInContext(UIGraphicsGetCurrentContext())
  7. let screenshot = UIGraphicsGetImageFromCurrentImageContext()
  8. UIGraphicsEndImageContext()
  9. let croppedImage = self.cropImage(screenshot)
  10. }
  11.  
  12. func cropImage(screenshot: UIImage) -> UIImage {
  13. let scale = screenshot.scale
  14. let imgSize = screenshot.size
  15. let screenHeight = UIScreen.mainScreen().applicationFrame.height
  16. let bound = self.view.bounds.height
  17. let navHeight = self.navigationController!.navigationBar.frame.height
  18. let bottomBarHeight = screenHeight - navHeight - bound
  19. let crop = CGRectMake(0,//"start" at the upper-left corner
  20. (imgSize.width - 1) * scale,//include half the width of the whole screen
  21. (imgSize.height - bottomBarHeight - 1) * scale) //include the height of the navigationBar and the height of view
  22.  
  23. let cgImage = CGImageCreateWithImageInRect(screenshot.CGImage,crop)
  24. let image: UIImage = UIImage(CGImage: cgImage)!
  25. return image
  26. }

猜你在找的iOS相关文章