要将Swift View截图变成img,你可以使用以下代码:

import UIKit

extension UIView {
    func takeScreenshot() -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)
        drawHierarchy(in: bounds, afterScreenUpdates: true)
        let screenshotImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return screenshotImage
    }
}

// 使用示例
if let screenshot = view.takeScreenshot() {
    // 将截图保存到相册
    UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)
    // 将截图显示在UIImageView上
    let imageView = UIImageView(image: screenshot)
    imageView.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
    view.addSubview(imageView)
}

这段代码定义了一个takeScreenshot的扩展方法,可以将一个UIView截图并返回UIImage对象。你可以将截图保存到相册,或者将截图显示在UIImageView上,如上述示例所示

swift view 截图变成img

原文地址: http://www.cveoy.top/t/topic/iUxU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录