我想将UIView转换为图像并将其保存在我的应用程序中.有人可以请告诉我如何截取视图的截图或将其转换为图像,以及将其保存在应用程序(不是相机胶卷)的最佳方法是什么?以下是视图的代码:
var overView = UIView(frame: CGRectMake(0, 0, self.view.frame.width/1.3, self.view.frame.height/1.3))
overView.center = CGPointMake(CGRectGetMidX(self.view.bounds),
CGRectGetMidY(self.view.bounds)-self.view.frame.height/16);
overView.backgroundColor = UIColor.whiteColor()
self.view.addSubview(overView)
self.view.bringSubviewToFront(overView)
Run Code Online (Sandbox Code Playgroud) 我正在尝试将 SwiftUI 视图呈现为 UIImage,然后让用户选择保存到相机胶卷或通过电子邮件发送给其他人。
例如,我想将 50 行的列表呈现到 UIImage 中。
struct MyList: View {
var body: some View {
List {
ForEach(0 ..< 50, id:\.self) {
Text("row \($0)")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
过去几周在互联网上搜索仍然没有运气。我尝试了 2 种不同的方法。
1. UIHostingController(来源在这里)
let hosting = UIHostingController(rootView: Text("TEST"))
hosting.view.frame = // Calculate the content size here //
let snapshot = hosting.view.snapshot // output: an empty snapshot of the size
print(hosting.view.subviews.count) // output: 0
Run Code Online (Sandbox Code Playgroud)
我试过layoutSubviews()
, setNeedsLayout()
, layoutIfNeeded()
, loadView()
,但结果仍然是 0 个子视图。 …
如何将视图(不是 UIView)转换为图像?此解决方案适用于可见区域。有什么方法可以将 swiftui 视图转换为图像。