【问题标题】:Problems with structs in Swift and making a UIImage from url?Swift 中的结构和从 url 制作 UIImage 的问题?
【发布时间】:2017-04-25 12:14:18
【问题描述】:

好吧,我不熟悉结构或我在 Swift 中处理的考验,但我需要做的是在我的 iMessage 应用程序扩展中创建一个 iMessage,其中包含一个贴纸,这意味着 iMessage 的图像部分是设置为贴纸。

我已经仔细研究了 Apple 的文档和 https://www.captechconsulting.com/blogs/ios-10-imessages-sdk-creating-an-imessages-extension,但我不明白如何做到这一点或结构的工作原理。我阅读了结构,但这并没有帮助我完成 Apple 在他们的示例代码中所做的事情(可在 Apple 下载)

Apple 所做的是他们首先编写一条消息,据我所知,将其结构作为属性,但我使用贴纸代替

guard let conversation = activeConversation else { fatalError("Expected a conversation") }
        //Create a new message with the same session as any currently selected message.
        let message = composeMessage(with: MSSticker, caption: "sup", session: conversation.selectedMessage?.session)

        // Add the message to the conversation.
        conversation.insert(message) { error in
            if let error = error {
                print(error)
            }
        }

然后他们这样做(这直接来自示例代码)来撰写消息:

   fileprivate func composeMessage(with iceCream: IceCream, caption: String, session: MSSession? = nil) -> MSMessage {
        var components = URLComponents()
        components.queryItems = iceCream.queryItems

        let layout = MSMessageTemplateLayout()
        layout.image = iceCream.renderSticker(opaque: true)
        layout.caption = caption

        let message = MSMessage(session: session ?? MSSession())
        message.url = components.url!
        message.layout = layout

        return message
    }
}

基本上这条线是我遇到的问题,因为我需要将我的贴纸设置为图像:

layout.image = iceCream.renderSticker(opaque: true)

Apple 做了一个我在renderSticker 中无法理解的复杂功能,将图像部分从他们的贴纸中提取出来,我已经尝试过他们的方法,但我认为这样更好:

let img = UIImage(contentsOfURL: square.imageFileURL)
        layout.image = ing

layout.image 需要一个 UIImage,我可以从贴纸中获取 imageFileURL,但我无法将它放入 UIImage。我收到一个错误,它与可用的重载不匹配。

我可以在这里做什么?如何将贴纸中的图像插入到消息中?如何从其 imageFileURL 中获取图像?

【问题讨论】:

  • 如果您的问题只是关于问题末尾附近的 UIImage 初始化程序,那么您为什么要发布所有其他不相关的信息?将您的问题集中在相关部分。
  • 对不起,我认为这一切都可能与 iMessage 和需要结构有关

标签: ios swift struct message image-file


【解决方案1】:

我不确定这个问题到底是什么,但我会尽量解决问题 --

正如 rmaddy 提到的,如果你想创建一个给定文件位置的图像,只需使用他指定的 UIImage 构造函数。

至于只发送一个贴纸(您在 rmaddy 的答案中的 cmets 中询问过),您可以只在 iMessage 对话中插入一个贴纸。此功能作为 MSConversation 的一部分提供。这是文档的链接:

https://developer.apple.com/reference/messages/msconversation/1648187-insert

可以从您的 MSMessagesAppViewController 访问活动对话。

【讨论】:

  • 如果我能投票给你两次,我会的。谢谢,跳过了文档中的内容
【解决方案2】:

UIImage 没有 init(contentsOfURL:) 初始化程序。最接近的是init(contentsOfFile:)

要将那个与您的文件 URL 一起使用,您可以这样做:

let img = UIImage(contentsOfFile: square.imageFileURL.path)

【讨论】:

  • 你知道有没有办法只发送贴纸,而不仅仅是图片?
  • 我对贴纸一无所知。
猜你喜欢
  • 2015-08-15
  • 1970-01-01
  • 2016-12-30
  • 2016-08-24
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2017-01-11
  • 2019-11-15
相关资源
最近更新 更多