首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >请求UIHostingController的视图返回空

请求UIHostingController的视图返回空
EN

Stack Overflow用户
提问于 2021-07-28 03:07:00
回答 1查看 35关注 0票数 1

我有一个分享按钮,应该分享一个表情包(图像+顶部文本+底部文本)。下面是关于这个按钮的代码:

代码语言:javascript
复制
Button(
                        action: {
                            items.removeAll()
                            items.append(createImage(from: UIHostingController(rootView: Meme(image: image, topText: topText, bottomText: bottomText)).view))
                            showingSharePage = true
                        }
                    ) {
                        Image(systemName: "square.and.arrow.up")
                            .font(.title)
                    }

如您所见,我将要共享的项目附加到项目中。我分享的是一个使用UIView生成的UIImage,该函数具有以下功能:

代码语言:javascript
复制
func createImage(from view: UIView) -> UIImage {
        
        UIGraphicsBeginImageContextWithOptions(CGSize(width: view.frame.width, height: view.frame.height), true, 1)
        view.layer.render(in: UIGraphicsGetCurrentContext()!)
        let generatedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return generatedImage!
}

但是,当我在模拟器或设备本身上运行时,我在"view.layer.render( in : UIGraphicsGetCurrentContext()!)“这一行中得到了这样的错误:"Fatal error: Unexpectedly nil when unwrapping an Optional value!”在createImage函数中。

这是我的meme结构:

代码语言:javascript
复制
struct Meme: View {
    @State var image: Image?
    @State var topText: String
    @State var bottomText: String
    
    var body: some View {
        image!
            .resizable()
            .aspectRatio(contentMode: .fit)
            .frame(width: UIScreen.main.bounds.size.width)
            .overlay(
                TextField("TOP", text: $topText)
                    .foregroundColor(.white)
                    .font(Font.custom("HelveticaNeue-CondensedBlack", size: 30))
                    .frame(width: UIScreen.main.bounds.size.width * 0.75)
                    .multilineTextAlignment(.center)
                    .padding(.vertical, 50.0)
                    .onTapGesture {
                        topText = ""
                    },
                alignment: .top
            )
            .overlay(
                TextField("BOTTOM", text: $bottomText)
                    .foregroundColor(.white)
                    .font(Font.custom("HelveticaNeue-CondensedBlack", size: 30))
                    .frame(width: UIScreen.main.bounds.size.width * 0.75)
                    .multilineTextAlignment(.center)
                    .padding(.vertical, 50.0)
                    .onTapGesture {
                        bottomText = ""
                    },
                alignment: .bottom
            )
    }
}

你知道为什么它返回nil吗?

EN

回答 1

Stack Overflow用户

发布于 2021-07-28 21:29:55

有趣的是,问题出在以下几行中

代码语言:javascript
复制
UIHostingController(rootView: Meme(image: image, topText: topText, bottomText: bottomText)).view)

代码语言:javascript
复制
UIGraphicsBeginImageContextWithOptions(CGSize(width: view.frame.width, height: view.frame.height), true, 1)

令我惊讶的是,UIHostingViewController返回的视图的初始框架是CGRect.zero

所以你实际上是在告诉上下文有一个0的点大小,显然它只是默默地失败了。可以通过在createImage:函数中打印大小来再次检查这一点

代码语言:javascript
复制
print(view.frame)

代码语言:javascript
复制
print(view.frame.size)

我很确定这就是问题所在。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68550512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档