Swift中的结构问题以及如何通过url制作UIImage? [英] Problems with structs in Swift and making a UIImage from url?

查看:255
本文介绍了Swift中的结构问题以及如何通过url制作UIImage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我不熟悉我在Swift中处理的结构或折磨,但是我需要做的是在我的iMessage应用扩展中创建一个带标签的iMessage,这意味着iMessage的图像部分是设置为贴纸.

Alright, I am not familiar with structs or the ordeal I am dealing with in Swift, but what I need to do is create an iMessage in my iMessage app extension with a sticker in it, meaning the image part of the iMessage is set to the sticker.

我已经仔细研究过Apple的文档和 https://www.captechconsulting.com/blogs/ios-10-imessages-sdk-creating-an-imessages-extension ,但我不知道该怎么做或结构实际上是如何工作的.我阅读了结构,但这并没有帮助我完成Apple在其示例代码(可从Apple下载)中所做的工作

I have pored over Apple's docs and https://www.captechconsulting.com/blogs/ios-10-imessages-sdk-creating-an-imessages-extension but I do not understand how to do this or really how structs work. I read up on structs but that has not helped me accomplishing what Apple does in their sample code (downloadable at Apple)

Apple所做的是他们首先编写一条消息,据我了解,该消息将其结构作为属性,但我改为贴上标签

What Apple does is they first compose a message, which I understood, taking their struct as a property, but I take sticker instead

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

然后他们执行此操作(直接来自示例代码)来编写消息:

They then do this (this is directly from sample code) to compose the message:

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

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

Basically this line is what Im having the problem with as I need to set my sticker as the image:

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

Apple做了一个整体上复杂的功能,在renderSticker中我不了解,可以将图像部分从贴纸上拉出来,我已经尝试过了,但是我认为这样做更好:

Apple does a whole complicated function thing that I don't understand in renderSticker to pull the image part out of their stickers, and I have tried their way but I think this is better:

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

layout.image需要一个UIImage,我可以从标签中获取imageFileURL,但是我不能将其放入UIImage中.我收到一个错误,提示它与可用的重载不匹配.

layout.image needs a UIImage, and I can get the imageFileURL from the sticker, I just cant get this into a UIImage. I get an error it does not match available overloads.

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

What can I do here? How can I insert the image from my sticker into a message? How can I get an image from its imageFileURL?

推荐答案

对于UIImage,没有init(contentsOfURL:)初始化程序.最接近的是init(contentsOfFile:).

There is no init(contentsOfURL:) initializer for UIImage. The closest one is init(contentsOfFile:).

要将其与您的文件网址一起使用,您可以执行以下操作:

To use that one with your file URL you can do:

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

这篇关于Swift中的结构问题以及如何通过url制作UIImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆