如何在 swift 中截取 UIView 的屏幕截图?
How to take screenshot of a UIView in swift?
我有一个名为 overView 的 UIView:
overView.frame = CGRectMake(self.view.frame.width/25, self.view.frame.height/25, self.view.frame.width/1.3, self.view.frame.height/1.2)
我只想截取此视图,而不是我的整个屏幕。并制作尺寸截图:
(CGSizeMake(2480,3508 )
这是我的代码:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(2480,3508 ), false, 0);
self.view.drawViewHierarchyInRect(CGRectMake(-self.view.frame.width/25, -self.view.frame.height/25,2480,3508), afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext()
屏幕截图符合要求的大小,但它截取了整个视图的屏幕截图,而不仅仅是 "overView"。
画一个视图,就用这个:
// Begin context
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.mainScreen().scale)
// Draw view in that context
drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
// And finally, get image
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
如果您想多次使用它,可能扩展会完成工作:
//Swift4
extension UIView {
func takeScreenshot() -> UIImage {
// Begin context
UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.main.scale)
// Draw view in that context
drawHierarchy(in: self.bounds, afterScreenUpdates: true)
// And finally, get image
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
if (image != nil)
{
return image!
}
return UIImage()
}
}
//旧Swift
extension UIView {
func takeScreenshot() -> UIImage {
// Begin context
UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.mainScreen().scale)
// Draw view in that context
drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
// And finally, get image
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
解释这些参数的作用:
UIGraphicsBeginImageContextWithOptions() creates a temporary rendering
context into which the original is drawn. The first argument, size, is
the target size of the scaled image. The second argument, isOpaque is
used to determine whether an alpha channel is rendered. Setting this
to false for images without transparency (i.e. an alpha channel) may
result in an image with a pink hue. The third argument scale is the
display scale factor. When set to 0.0, the scale factor of the main
screen is used, which for Retina displays is 2.0 or higher (3.0 on the
iPhone 6 Plus).
更多信息请点击此处http://nshipster.com/image-resizing/
swift 4 和 iOS 10+
extension UIView {
func screenshot() -> UIImage {
return UIGraphicsImageRenderer(size: bounds.size).image { _ in
drawHierarchy(in: CGRect(origin: .zero, size: bounds.size), afterScreenUpdates: true)
}
}
}
要使屏幕变短,请使用此解决方案。听说我可以用 UIImage
.
完成如何缩短屏幕 UIView
let img = self.captureScreen() // CaptureScreen Is A Function
let someNSDate = NSDate()
let myTimeStamp = someNSDate.timeIntervalSince1970
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
if let data = UIImageJPEGRepresentation(img, 0.8)
{
let filename = documentsDirectory.appendingPathComponent("Img_\(myTimeStamp).jpeg")
SavedImage_Ary.insert("Img_\(myTimeStamp).jpeg", at: 0) // SavedImage Is A NSMutableArray Where You Can Store your Image
//print(SavedImage_Ary)
try? data.write(to: filename)
UserDefaults.standard.setValue(SavedImage_Ary, forKey: "Saved_Image")
Save_Img = true
// self.dismiss(animated: true, completion: nil)
Select_Flag = "textdata"
let vc = self.storyboard?.instantiateViewController(withIdentifier: "photovc") as! UINavigationController
self.present(vc, animated: true, completion: nil)
}
// MARK : Function
func captureScreen() -> UIImage
{
UIGraphicsBeginImageContextWithOptions(Capture_View.frame.size, false, 0);
Capture_View.drawHierarchy(in: Capture_View.bounds, afterScreenUpdates: true)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
Swift 4.x 和 iOS 10+ 后备解决方案:
extension UIView {
func screenshot() -> UIImage {
if #available(iOS 10.0, *) {
return UIGraphicsImageRenderer(size: bounds.size).image { _ in
drawHierarchy(in: CGRect(origin: .zero, size: bounds.size), afterScreenUpdates: true)
}
} else {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.main.scale)
drawHierarchy(in: self.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()
UIGraphicsEndImageContext()
return image
}
}
}
的另一种选择,更简洁 Swift 风格:
extension UIView {
var snapshot: UIImage {
return UIGraphicsImageRenderer(size: bounds.size).image { _ in
drawHierarchy(in: bounds, afterScreenUpdates: true)
}
}
}
func screenShotMethod()->UIImage
{
let layer = self.view.layer
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
layer.render(in: UIGraphicsGetCurrentContext()!)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return screenshot!
}
我有一个名为 overView 的 UIView:
overView.frame = CGRectMake(self.view.frame.width/25, self.view.frame.height/25, self.view.frame.width/1.3, self.view.frame.height/1.2)
我只想截取此视图,而不是我的整个屏幕。并制作尺寸截图:
(CGSizeMake(2480,3508 )
这是我的代码:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(2480,3508 ), false, 0);
self.view.drawViewHierarchyInRect(CGRectMake(-self.view.frame.width/25, -self.view.frame.height/25,2480,3508), afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext()
屏幕截图符合要求的大小,但它截取了整个视图的屏幕截图,而不仅仅是 "overView"。
画一个视图,就用这个:
// Begin context
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.mainScreen().scale)
// Draw view in that context
drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
// And finally, get image
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
如果您想多次使用它,可能扩展会完成工作:
//Swift4
extension UIView {
func takeScreenshot() -> UIImage {
// Begin context
UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.main.scale)
// Draw view in that context
drawHierarchy(in: self.bounds, afterScreenUpdates: true)
// And finally, get image
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
if (image != nil)
{
return image!
}
return UIImage()
}
}
//旧Swift
extension UIView {
func takeScreenshot() -> UIImage {
// Begin context
UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.mainScreen().scale)
// Draw view in that context
drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
// And finally, get image
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
解释这些参数的作用:
UIGraphicsBeginImageContextWithOptions() creates a temporary rendering context into which the original is drawn. The first argument, size, is the target size of the scaled image. The second argument, isOpaque is used to determine whether an alpha channel is rendered. Setting this to false for images without transparency (i.e. an alpha channel) may result in an image with a pink hue. The third argument scale is the display scale factor. When set to 0.0, the scale factor of the main screen is used, which for Retina displays is 2.0 or higher (3.0 on the iPhone 6 Plus).
更多信息请点击此处http://nshipster.com/image-resizing/
swift 4 和 iOS 10+
extension UIView {
func screenshot() -> UIImage {
return UIGraphicsImageRenderer(size: bounds.size).image { _ in
drawHierarchy(in: CGRect(origin: .zero, size: bounds.size), afterScreenUpdates: true)
}
}
}
要使屏幕变短,请使用此解决方案。听说我可以用 UIImage
.
UIView
let img = self.captureScreen() // CaptureScreen Is A Function
let someNSDate = NSDate()
let myTimeStamp = someNSDate.timeIntervalSince1970
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
if let data = UIImageJPEGRepresentation(img, 0.8)
{
let filename = documentsDirectory.appendingPathComponent("Img_\(myTimeStamp).jpeg")
SavedImage_Ary.insert("Img_\(myTimeStamp).jpeg", at: 0) // SavedImage Is A NSMutableArray Where You Can Store your Image
//print(SavedImage_Ary)
try? data.write(to: filename)
UserDefaults.standard.setValue(SavedImage_Ary, forKey: "Saved_Image")
Save_Img = true
// self.dismiss(animated: true, completion: nil)
Select_Flag = "textdata"
let vc = self.storyboard?.instantiateViewController(withIdentifier: "photovc") as! UINavigationController
self.present(vc, animated: true, completion: nil)
}
// MARK : Function
func captureScreen() -> UIImage
{
UIGraphicsBeginImageContextWithOptions(Capture_View.frame.size, false, 0);
Capture_View.drawHierarchy(in: Capture_View.bounds, afterScreenUpdates: true)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
Swift 4.x 和 iOS 10+ 后备解决方案:
extension UIView {
func screenshot() -> UIImage {
if #available(iOS 10.0, *) {
return UIGraphicsImageRenderer(size: bounds.size).image { _ in
drawHierarchy(in: CGRect(origin: .zero, size: bounds.size), afterScreenUpdates: true)
}
} else {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.main.scale)
drawHierarchy(in: self.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()
UIGraphicsEndImageContext()
return image
}
}
}
extension UIView {
var snapshot: UIImage {
return UIGraphicsImageRenderer(size: bounds.size).image { _ in
drawHierarchy(in: bounds, afterScreenUpdates: true)
}
}
}
func screenShotMethod()->UIImage
{
let layer = self.view.layer
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
layer.render(in: UIGraphicsGetCurrentContext()!)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return screenshot!
}