我试图在视频消息气泡(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
}
}
我希望有人能帮助我
我为此做了一个扩展
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;
我希望它对某人有用