下面是MessageKit库的示例代码。当前,其行为是在向最终用户显示之前必须完全加载所有图像消息,因此需要大量时间等待。我想要的是在所有消息都已加载之后加载图像。

看1 /,您将看到“kind”变量将是一个mediaItem
看2 /,您将看到mediaItem具有“url”变量
看3 /,您将看到消息是使用上述mediaItem创建的

问题是如何在3 /处获取消息的URL?我在下面尝试过,但迅速不明白

let imkind = message.kind
imkind.photo.url // error Enum element 'photo' cannot be referenced as an instance member

以下是1/2/3 /

1 /消息类型协议
public protocol MessageType {

    /// The sender of the message.
    var sender: Sender { get }

    /// The unique identifier for the message.
    var messageId: String { get }

    /// The date the message was sent.
    var sentDate: Date { get }

    /// The kind of message and its underlying kind.
    var kind: MessageKind { get }

}

2 /创建一个图像以放入消息种类如下:
private struct ImageMediaItem: MediaItem {

    var url: URL?
    var image: UIImage?
    var placeholderImage: UIImage
    var size: CGSize

    init(image: UIImage) {
        self.image = image
        self.size = CGSize(width: 240, height: 240)
        self.placeholderImage = UIImage()
    }

}

3 /根据上述MessageType创建一条消息:
internal struct MockMessage: MessageType {

    var messageId: String
    var sender: Sender
    var sentDate: Date
    var kind: MessageKind

    private init(kind: MessageKind, sender: Sender, messageId: String, date: Date) {
        self.kind = kind
        self.sender = sender
        self.messageId = messageId
        self.sentDate = date
    }

    init(image: UIImage, sender: Sender, messageId: String, date: Date) {
        let mediaItem = ImageMediaItem(image: image)
        self.init(kind: .photo(mediaItem), sender: sender, messageId: messageId, date: date)
    }

}

4 / messageKind在pod库中
/// An enum representing the kind of message and its underlying kind.
public enum MessageKind {

    /// A standard text message.
    ///
    /// - Note: The font used for this message will be the value of the
    /// `messageLabelFont` property in the `MessagesCollectionViewFlowLayout` object.
    ///
    /// Using `MessageKind.attributedText(NSAttributedString)` doesn't require you
    /// to set this property and results in higher performance.
    case text(String)

    /// A message with attributed text.
    case attributedText(NSAttributedString)

    /// A photo message.
    case photo(MediaItem)

    /// A video message.
    case video(MediaItem)

    /// A location message.
    case location(LocationItem)

    /// An emoji message.
    case emoji(String)

    /// A custom message.
    /// - Note: Using this case requires that you implement the following methods and handle this case:
    ///   - MessagesDataSource: customCell(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UICollectionViewCell
    ///   - MessagesLayoutDelegate: customCellSizeCalculator(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CellSizeCalculator
    case custom(Any?)

}

最佳答案

guard case .photo(let mediaItem) = imkind else {
        return
    }

    print(mediaItem.url)

您必须使用此代码解开枚举大小写。问题是您尝试将枚举检索为mediaItem。但是您必须检索内部数据。

08-19 08:02