如何异步加载图像

时间:2016-04-05 03:04:18

标签: ios swift jsqmessagesviewcontroller

目前这是我在viewDidLoad中的方法中的实现     dateParameter = self.lastMessageDate

        for chat in self.realm.objects(ChatObject).filter("room_id == \(self.room.id)").sorted("dateTime", ascending: true) {

            if chat.type == "GIFT" {
                print(chat.gift)
                let imageData = NSData(contentsOfURL: NSURL(string: chat.gift!.image)!)
                //let photoItem = JSQPhotoMediaItem(image: UIImage(data: imageData!))
                let photoItem = ChatImageJSQPhoto(image: UIImage(data: imageData!))

                let photoMessage = JSQMessage(senderId: String(chat.sender!.id), senderDisplayName: chat.sender!.username, date: self.formatter.dateFromString(chat.dateTime), media: photoItem)
                self.messages.append(photoMessage)
                //then get the message if it exists
                if chat.desc.characters.count > 0 {
                    self.addMessage(String(chat.sender!.id), senderName: chat.sender!.username, text: chat.desc, date: chat.dateTime)
                }
            } else {
                if chat.desc.characters.count > 0 {
                    print(chat.sender?.username)
                    self.addMessage(String(chat.sender!.id), senderName: chat.sender!.username, text: chat.desc, date: chat.dateTime)
                }
            }
        }
        self.finishReceivingMessage()

但是,正如您可能预测的那样,在进入我的JSQMessagesViewController之前,我会被困住几秒钟。 如何加载这些气泡并在气泡中间有一个活动指示器,然后异步加载图像?

1 个答案:

答案 0 :(得分:1)

我更喜欢使用翠鸟。相信我这很棒

        let resource = ImageResource(downloadURL:URL(string: productsForSearch.result[indexPath.row].image_url )!,cacheKey: productsForSearch.result[indexPath.row].image_url )
        CollectionCell.imagesForSearch.kf.indicatorType = .activity //this is to make activity indicator
        CollectionCell.imagesForSearch.kf.setImage(with: resource)