显示在JSQMessagesViewController中发送的视频的图像预览

时间:2018-04-11 10:24:37

标签: ios swift firebase video jsqmessagesviewcontroller

我试图在视频消息(JSQVideoMediaItem)的气泡中显示用户发送的视频的图像预览,但我的代码仅显示黑色预览。 我确定函数generateThumbnail()在我使用它时可以正常工作,以便获取从Firebase发送的视频文件。 我使用Firebase存储,当用户发送视频时我将其存储在Firebase中,因此预览从Firebase存储中选择了正确的视频文件,我使用print(previewImage)对其进行了测试。

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell
    let message = messages[indexPath.item]

    if !message.isMediaMessage {
        if message.senderId == senderId {
            cell.textView.textColor = UIColor.white
        } else {
            cell.textView.textColor = UIColor.black
        }
    } else if message.isMediaMessage {
        if let media = message.media as? JSQVideoMediaItem {
            var imagePreview = generateThumbnail(url: (media.fileURL)!)
            let imageView = UIImageView(image: imagePreview)
            imageView.backgroundColor = UIColor.clear
            imageView.frame = CGRect(x: 0.0, y: 0.0, width: view.frame.size.width, height: view.frame.size.height)
            imageView.contentMode = .center
            imageView.clipsToBounds = true
            JSQMessagesMediaViewBubbleImageMasker.applyBubbleImageMask(toMediaView: imageView, isOutgoing: false)
        }
    }
    return cell
}

func generateThumbnail(url: URL) -> UIImage? {
    do {
        let asset = AVURLAsset(url: url)
        let imageGenerator = AVAssetImageGenerator(asset: asset)
        imageGenerator.appliesPreferredTrackTransform = true
        let time: CMTime = CMTimeMake(1, 1)
        let cgImage = try imageGenerator.copyCGImage(at: time, actualTime: nil)
        return UIImage(cgImage: cgImage)
    } catch {
        print(error.localizedDescription)
        return nil
    }
}

我希望有人可以帮助我

1 个答案:

答案 0 :(得分:0)

我对此做了扩展

    extension JSQVideoMediaItem {

    func addThumbnail() {

        var img: UIImage? = nil

        /// create thumbnail

            do {
                let asset = AVURLAsset(url: self.fileURL)
                let imgGenerator = AVAssetImageGenerator(asset: asset)
                imgGenerator.appliesPreferredTrackTransform = true

                let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(0,1), actualTime: nil)
                img = UIImage(cgImage: cgImage)
            }
            catch let error as NSError {
                print("Image generator error :  \(error.localizedDescription)")
            }

        /// create thumbnail END

        let size:CGSize = self.mediaViewDisplaySize()

        let playIcon = UIImage.jsq_defaultPlay()?.jsq_imageMasked(with: .white)
        let imageView:UIImageView = UIImageView(image: playIcon)
        imageView.frame = CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height)
        imageView.contentMode = UIViewContentMode.center
        imageView.clipsToBounds = true
        JSQMessagesMediaViewBubbleImageMasker.applyBubbleImageMask(toMediaView: imageView, isOutgoing: self.appliesMediaViewMaskAsOutgoing)

        if (img != nil) {
            let thumbnailImageView:UIImageView = UIImageView(image: img)
            thumbnailImageView.frame = CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height)
            thumbnailImageView.contentMode = UIViewContentMode.scaleAspectFill
            thumbnailImageView.clipsToBounds = true
            JSQMessagesMediaViewBubbleImageMasker.applyBubbleImageMask(toMediaView: thumbnailImageView, isOutgoing: self.appliesMediaViewMaskAsOutgoing)

            imageView.backgroundColor = .clear
            thumbnailImageView.addSubview(imageView)
            self.cachedVideoImageView = thumbnailImageView;

        }
        else {
            imageView.backgroundColor = .black
            self.cachedVideoImageView = imageView
        }
    }
}
创建JSQVideoMediaItem后

addThumbnail

vidItem = JSQVideoMediaItem(fileURL: URL(fileURLWithPath: destPath), isReadyToPlay: true)
vidItem?.addThumbnail()

,您应将此行JSQVideoMediaItem.m移至JSQVideoMediaItem.h以进行公共访问

@property (strong, nonatomic) UIImageView *cachedVideoImageView;

我希望这对某人有用