|
@@ -31,18 +31,19 @@ class BFVideoThumbProgressView: UIView {
|
|
|
var dragStartHandle: (() -> Void)?
|
|
|
var isDrag = false
|
|
|
|
|
|
- let thumbImageWidth: CGFloat = 70.0
|
|
|
+ let thumbImageWidth : CGFloat = 70.0
|
|
|
|
|
|
let fetchThumbStrategy: BFVideoThumbProgressStrategyProtocol = BFVideoThumbProgressStrategy()
|
|
|
|
|
|
- var progress: Double = 0 { // 进度,秒为单位
|
|
|
+ var progress: Double = 0 { // 进度,秒为单位
|
|
|
didSet {
|
|
|
updateProgress(progress: progress)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
var thumbImgs = [UIImage]()
|
|
|
-
|
|
|
+ var lastImg :UIImageView?
|
|
|
+
|
|
|
lazy var progressView: BFAutolayoutScrollView = {
|
|
|
let sv = BFAutolayoutScrollView()
|
|
|
sv.bounces = false
|
|
@@ -76,7 +77,7 @@ class BFVideoThumbProgressView: UIView {
|
|
|
make.width.equalTo(3)
|
|
|
make.center.height.equalToSuperview()
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
progressView.contentView.addSubview(progessIndicateBackV)
|
|
|
progessIndicateBackV.snp.makeConstraints { make in
|
|
|
make.left.equalTo(width * 0.5)
|
|
@@ -103,11 +104,11 @@ class BFVideoThumbProgressView: UIView {
|
|
|
func dealWithVideoThumb() {
|
|
|
guard let videoAsset = recordItem?.videoAsset else {
|
|
|
// 可能icloud资源没回来,清空原有内容
|
|
|
- addThumbImages(images: [UIImage]())
|
|
|
+ self.addThumbImages(images: [UIImage]())
|
|
|
return
|
|
|
}
|
|
|
- addThumbImages(images: recordItem!.thumbImgs)
|
|
|
- if recordItem!.thumbImgs.count > 0 {
|
|
|
+ self.addThumbImages(images: recordItem!.thumbImgs)
|
|
|
+ if recordItem!.thumbImgs.count > 0{
|
|
|
// 代表已经获取过了,不用在重新去获得
|
|
|
progessIndicateBackV.snp.remakeConstraints { make in
|
|
|
make.left.equalTo(width * 0.5)
|
|
@@ -118,7 +119,7 @@ class BFVideoThumbProgressView: UIView {
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
let date = Date()
|
|
|
let dur = videoAsset.duration.seconds
|
|
|
if dur > 0 {
|
|
@@ -130,23 +131,24 @@ class BFVideoThumbProgressView: UIView {
|
|
|
make.height.equalTo(6)
|
|
|
make.width.equalTo(CGFloat(count) * thumbImageWidth).priority(.high)
|
|
|
}
|
|
|
-
|
|
|
- recordItem!.splitVideoFileUrlFps(frames: count, firstImagesCount: Int(ceil(width / 2.0 / thumbImageWidth))) { [weak self, weak recordItem] hadGetAll, images in
|
|
|
+
|
|
|
+
|
|
|
+ recordItem!.splitVideoFileUrlFps(frames: count, firstImagesCount: Int(ceil(width/2.0/thumbImageWidth))) { [weak self, weak recordItem] hadGetAll, images in
|
|
|
guard let sself = self, let sitem = recordItem else {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- BFLog(1, message: "获取缩略图:\(hadGetAll), \(Date().timeIntervalSince(date)), \(sitem.localPath ?? "aa")")
|
|
|
+ BFLog(1, message: "获取缩略图:\(hadGetAll), \(Date().timeIntervalSince(date)), \( sitem.localPath ?? "aa")")
|
|
|
sitem.thumbImgs.removeAll()
|
|
|
sitem.thumbImgs.append(contentsOf: images)
|
|
|
-
|
|
|
+
|
|
|
// 不足数则补充足够帧数
|
|
|
- while hadGetAll, sitem.thumbImgs.count < count, images.count > 0 {
|
|
|
+ while (hadGetAll && sitem.thumbImgs.count < count && images.count > 0) {
|
|
|
sitem.thumbImgs.append(images.last!)
|
|
|
}
|
|
|
if sitem.localPath == sself.recordItem!.localPath {
|
|
|
sself.addThumbImages(images: sitem.thumbImgs)
|
|
|
- } else {
|
|
|
+ }else{
|
|
|
BFLog(1, message: "thumbImgs.count:\(sitem.thumbImgs.count)")
|
|
|
}
|
|
|
}
|
|
@@ -159,7 +161,7 @@ class BFVideoThumbProgressView: UIView {
|
|
|
addThumbImages(images: [UIImage]())
|
|
|
return
|
|
|
}
|
|
|
- addThumbImages(images: Array(repeating: image, count: 30))
|
|
|
+ addThumbImages(images: Array(repeating: image, count: 10))
|
|
|
}
|
|
|
|
|
|
/// 添加缩略图
|
|
@@ -172,9 +174,10 @@ class BFVideoThumbProgressView: UIView {
|
|
|
}
|
|
|
}
|
|
|
if images.count > 0 {
|
|
|
-// self?.thumbImgs = images
|
|
|
if let sself = self {
|
|
|
- var lastImg = UIImageView()
|
|
|
+ if sself.lastImg != nil && sself.lastImg?.superview != nil {
|
|
|
+ sself.lastImg?.removeFromSuperview()
|
|
|
+ }
|
|
|
for (i, img) in images.enumerated() {
|
|
|
let iv = UIImageView(image: img)
|
|
|
iv.contentMode = .scaleAspectFill
|
|
@@ -186,19 +189,12 @@ class BFVideoThumbProgressView: UIView {
|
|
|
make.height.equalTo(50)
|
|
|
make.width.equalTo(sself.thumbImageWidth)
|
|
|
}
|
|
|
- lastImg = iv
|
|
|
+ sself.lastImg = iv
|
|
|
}
|
|
|
if sself.recordItem?.mediaType == .IMAGE {
|
|
|
// 图片需要动态修改宽度
|
|
|
- lastImg.snp.makeConstraints { make in
|
|
|
+ sself.lastImg?.snp.makeConstraints { make in
|
|
|
make.right.equalTo(sself.width * -0.5)
|
|
|
- make.height.equalTo(50)
|
|
|
- make.width.equalTo(sself.thumbImageWidth)
|
|
|
- }
|
|
|
- DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) { [weak self] in
|
|
|
- lastImg.snp.updateConstraints { make in
|
|
|
- make.width.equalTo(self?.thumbImageWidth ?? 70)
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -209,12 +205,14 @@ class BFVideoThumbProgressView: UIView {
|
|
|
func appendThumb(progress: Double = 0) {
|
|
|
let count: Int = Int(progress / 2)
|
|
|
BFLog(message: "需要的图片个数:progress=\(progress),count=\(count)")
|
|
|
- if recordItem?.mediaType == .IMAGE, (thumbImgs.count - 5) < count {
|
|
|
+ if recordItem?.mediaType == .IMAGE, (progressView.contentView.subviews.count - 6) < count {
|
|
|
guard let image = recordItem?.coverImg else {
|
|
|
return
|
|
|
}
|
|
|
- var lastiv = UIImageView()
|
|
|
- let lastIndex = thumbImgs.count - 1
|
|
|
+ if lastImg != nil && lastImg?.superview != nil {
|
|
|
+ lastImg?.removeFromSuperview()
|
|
|
+ }
|
|
|
+ let lastIndex = progressView.contentView.subviews.count - 1
|
|
|
for i in lastIndex ... lastIndex + 10 {
|
|
|
let iv = UIImageView(image: image)
|
|
|
iv.contentMode = .scaleAspectFill
|
|
@@ -226,18 +224,10 @@ class BFVideoThumbProgressView: UIView {
|
|
|
make.height.equalTo(50)
|
|
|
make.width.equalTo(thumbImageWidth)
|
|
|
}
|
|
|
- lastiv = iv
|
|
|
- thumbImgs.append(image)
|
|
|
+ lastImg = iv
|
|
|
}
|
|
|
- lastiv.snp.makeConstraints { make in
|
|
|
+ lastImg?.snp.makeConstraints { make in
|
|
|
make.right.equalTo(width * -0.5)
|
|
|
- make.height.equalTo(50)
|
|
|
- make.width.equalTo(thumbImageWidth)
|
|
|
- }
|
|
|
- DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) { [weak self] in
|
|
|
- lastiv.snp.updateConstraints { make in
|
|
|
- make.width.equalTo(self?.thumbImageWidth ?? 70)
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -266,39 +256,39 @@ class BFVideoThumbProgressView: UIView {
|
|
|
|
|
|
extension BFVideoThumbProgressView: UIScrollViewDelegate {
|
|
|
func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
|
|
- let totalW = recordItem?.mediaType == .VIDEO ? (scrollView.contentSize.width - width) : (CGFloat(recordItem?.materialDuraion ?? 0) * thumbImageWidth / 2.0)
|
|
|
+ let totalW = recordItem?.mediaType == .VIDEO ? (scrollView.contentSize.width - width) : (CGFloat(recordItem?.materialDuraion ?? 0) * thumbImageWidth / 2.0)
|
|
|
if recordItem?.mediaType == .VIDEO {
|
|
|
if isDrag {
|
|
|
- dragScrollProgressHandle?(false, totalW > 0 ? Float(scrollView.contentOffset.x / totalW) : 0)
|
|
|
+ dragScrollProgressHandle?(false, totalW > 0 ? Float((scrollView.contentOffset.x / totalW)) : 0)
|
|
|
}
|
|
|
} else if recordItem?.mediaType == .IMAGE {
|
|
|
if isDrag {
|
|
|
if scrollView.contentOffset.x > CGFloat(recordItem?.materialDuraion ?? 0) * thumbImageWidth / 2.0 {
|
|
|
scrollView.contentOffset = CGPoint(x: CGFloat(recordItem?.materialDuraion ?? 0) * thumbImageWidth / 2.0, y: 0)
|
|
|
}
|
|
|
- dragScrollProgressHandle?(false, totalW > 0 ? Float(scrollView.contentOffset.x / totalW) : 0)
|
|
|
+ dragScrollProgressHandle?(false, totalW > 0 ? Float((scrollView.contentOffset.x / totalW)) : 0)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
|
|
|
isDrag = true
|
|
|
- let totalW = recordItem?.mediaType == .VIDEO ? (scrollView.contentSize.width - width) : (CGFloat(recordItem?.materialDuraion ?? 0) * thumbImageWidth / 2.0)
|
|
|
+ let totalW = recordItem?.mediaType == .VIDEO ? (scrollView.contentSize.width - width) : (CGFloat(recordItem?.materialDuraion ?? 0) * thumbImageWidth / 2.0)
|
|
|
dragStartHandle?()
|
|
|
- dragScrollProgressHandle?(true, totalW > 0 ? Float(scrollView.contentOffset.x / totalW) : 0)
|
|
|
+ dragScrollProgressHandle?(true, totalW > 0 ? Float((scrollView.contentOffset.x / totalW)) : 0)
|
|
|
}
|
|
|
|
|
|
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
|
|
|
if !decelerate {
|
|
|
- let totalW = recordItem?.mediaType == .VIDEO ? (scrollView.contentSize.width - width) : (CGFloat(recordItem?.materialDuraion ?? 0) * thumbImageWidth / 2.0)
|
|
|
+ let totalW = recordItem?.mediaType == .VIDEO ? (scrollView.contentSize.width - width) : (CGFloat(recordItem?.materialDuraion ?? 0) * thumbImageWidth / 2.0)
|
|
|
isDrag = false
|
|
|
- dragEndHandle?(totalW > 0 ? Float(scrollView.contentOffset.x / totalW) : 0)
|
|
|
+ dragEndHandle?(totalW > 0 ? Float((scrollView.contentOffset.x / totalW)) : 0)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
|
|
|
- let totalW = recordItem?.mediaType == .VIDEO ? (scrollView.contentSize.width - width) : (CGFloat(recordItem?.materialDuraion ?? 0) * thumbImageWidth / 2.0)
|
|
|
+ let totalW = recordItem?.mediaType == .VIDEO ? (scrollView.contentSize.width - width) : (CGFloat(recordItem?.materialDuraion ?? 0) * thumbImageWidth / 2.0)
|
|
|
isDrag = false
|
|
|
- dragEndHandle?(totalW > 0 ? Float(scrollView.contentOffset.x / totalW) : 0)
|
|
|
+ dragEndHandle?(totalW > 0 ? Float((scrollView.contentOffset.x / totalW)) : 0)
|
|
|
}
|
|
|
}
|