Problems with structs in Swift and making a UIImage from url?

╄→尐↘猪︶ㄣ 提交于 2019-12-12 04:34:26

问题


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.

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)

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

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?


回答1:


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)



回答2:


I'm not sure what exactly the question is, but I'll try to address as much as I can --

As rmaddy mentioned, if you want to create an image given a file location, simply use the UIImage constructor he specified.

As far as sending just a sticker (which you asked about in the comments on rmaddy's answer), you can insert just a sticker into an iMessage conversation. This functionality is available as part of an MSConversation. Here is a link to the documentation:

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

The active conversation can be accessed from your MSMessagesAppViewController.



来源:https://stackoverflow.com/questions/41070206/problems-with-structs-in-swift-and-making-a-uiimage-from-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!