ios 如何在Swift中截图UIView?

khbbv19g  于 2023-08-08  发布在  iOS
关注(0)|答案(7)|浏览(159)

我有一个名为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”。

cxfofazt

cxfofazt1#

要绘制一个视图,只需使用以下命令:

// 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()

字符串
如果你想多次使用它,扩展可能会完成这项工作:
//快速4

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()
    }
}


//老雨燕

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()创建一个临时渲染上下文,在该上下文中绘制原始对象。第一个参数size是缩放图像的目标大小。“第二个参数isOpaque用于确定是否渲染Alpha通道。如果图像不具有透明度(即阿尔法通道)可以导致具有粉红色调的图像。第三个参数scale是显示比例系数。当设定为0.0时,将使用主屏幕的比例因子,对于Retina显示器,该比例因子为2.0或更高(在iPhone 6 Plus上为3.0)。
更多信息请点击此处http://nshipster.com/image-resizing/
至于draw调用,Apple Docs在此详细解释,here

d8tt03nd

d8tt03nd2#

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

}

字符串

p1tboqfb

p1tboqfb3#

Alessandro的另一个答案,更简短和Swift风格:

extension UIView {

    var snapshot: UIImage {
        return UIGraphicsImageRenderer(size: bounds.size).image { _ in
            drawHierarchy(in: bounds, afterScreenUpdates: true)
        }
    }

}

字符串

u4vypkhs

u4vypkhs4#

对于屏幕短使用此解决方案。听到我可以完成如何使用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
}

字符串

vpfxa7rd

vpfxa7rd5#

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

字符串

m3eecexj

m3eecexj6#

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
        }
    }
}

字符串

ocebsuys

ocebsuys7#

如果你不是特别需要UIImage,但UIView就可以了,你可以用途:

let snapshotView = yourView.snapshotView(afterScreenUpdates: true)

字符串
https://developer.apple.com/documentation/uikit/uiview/1622531-snapshotview

相关问题