iOS Swift-可编程聊天媒体消息未下载

时间:2018-08-09 15:40:00

标签: swift twilio twilio-programmable-chat

昨晚,我能够实现可编程聊天SDK,以便通过媒体消息功能发送和接收图片。我上床睡觉时工作得很漂亮。今天早上醒来进行处理,并且不再下载媒体消息。

在此特定频道中,我连续收到4条媒体消息,并且客户端似乎尝试下载5次,直到它停止并且不提供任何完成或错误消息。当我重新启动该应用并加载包含1条媒体消息的频道时,它将下载并正确显示。

您是否认为给定频道的下载量过多?也许客户处理不了那么多,需要排队吗?

任何帮助将不胜感激!

我的代码已从Twilio文档中撤出,但是无论如何我都会将其发布在这里:

if (message.hasMedia()) {
            print("Message has media")
            //handle media
            let cell = tableView.dequeueReusableCell(withIdentifier: "ImageCell", for: indexPath) as! ImageChatTableViewCell
            if let name = message.attributes()!["friendly_name"] {
                print("atribute found")
                cell.msgSubtitle.text = name as! String
            } else {
                print("No attributes found")
                cell.msgSubtitle.text = message.author
            }

            let tempFilename = (NSTemporaryDirectory() as NSString).appendingPathComponent(message.mediaFilename ?? "file.dat")
            let outputStream = OutputStream(toFileAtPath: tempFilename, append: false)

            // Request the start of the download
            if let outputStream = outputStream {
                message.getMediaWith(outputStream,
                                     onStarted: {
                                        // Called when download of media begins.
                                        print("Media download started")
                },
                                     onProgress: { (bytes) in
                                        // Called as download progresses, with the current byte count.
                                        print("Media download progress:  \(bytes)")
                },
                                     onCompleted: { (mediaSid) in
                                        // Called when download is completed, with the new mediaSid if successful.
                                        // Full failure details will be provided through the completion block below.

                }) { (result) in
                    if !result.isSuccessful() {
                        print("Download failed: \(String(describing: result.error))")
                    } else {
                        print("Image Download successful")

                        let file: FileHandle? = FileHandle(forReadingAtPath: tempFilename)

                        if file != nil {
                            // Read all the data
                            let data = file?.readDataToEndOfFile()

                            // Close the file
                            file?.closeFile()

                            print("DATA: \(data!)")
                            let image = UIImage(data: data!)
                            cell.msgImageView.image = image
                        } else {
                            print("Ooops! Something went wrong!")
                        }

                    }
                }
            }

            return cell
        }

0 个答案:

没有答案
相关问题