|
@@ -47,9 +47,9 @@ class PQPHAssetVideoParaseUtil: NSObject {
|
|
|
/// - asset: <#asset description#>
|
|
|
/// - resultHandler: <#resultHandler description#>
|
|
|
/// - Returns: <#description#>
|
|
|
- class func parasToAVPlayerItem(phAsset: PHAsset,isHighQuality : Bool = false, resultHandler: @escaping (AVPlayerItem?, Float64, [AnyHashable: Any]?) -> Void) {
|
|
|
+ class func parasToAVPlayerItem(phAsset: PHAsset, isHighQuality: Bool = false, resultHandler: @escaping (AVPlayerItem?, Float64, [AnyHashable: Any]?) -> Void) {
|
|
|
PHImageManager().requestPlayerItem(forVideo: phAsset, options: videoRequestOptions) { playerItem, info in
|
|
|
- if isHighQuality && (playerItem?.asset as? AVURLAsset)?.url.absoluteString.components(separatedBy: "/").last?.contains(".medium.") ?? false{
|
|
|
+ if isHighQuality, (playerItem?.asset as? AVURLAsset)?.url.absoluteString.components(separatedBy: "/").last?.contains(".medium.") ?? false {
|
|
|
let tempVideoOptions = PHVideoRequestOptions()
|
|
|
tempVideoOptions.version = .original
|
|
|
// 下载iCloud视频
|
|
@@ -58,12 +58,12 @@ class PQPHAssetVideoParaseUtil: NSObject {
|
|
|
tempVideoOptions.progressHandler = { progress, error, pointer, info in
|
|
|
BFLog(message: "导出playerItem-progress = \(progress),error = \(String(describing: error)),pointer = \(pointer),info = \(String(describing: info))")
|
|
|
}
|
|
|
- PHImageManager().requestPlayerItem(forVideo: phAsset, options: tempVideoOptions) { (playerItem, info) in
|
|
|
+ PHImageManager().requestPlayerItem(forVideo: phAsset, options: tempVideoOptions) { playerItem, info in
|
|
|
let size = try! (playerItem?.asset as? AVURLAsset)?.url.resourceValues(forKeys: [.fileSizeKey])
|
|
|
BFLog(message: "size = \(String(describing: size))")
|
|
|
resultHandler(playerItem, Float64(size?.fileSize ?? 0), info)
|
|
|
}
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
let size = try! (playerItem?.asset as? AVURLAsset)?.url.resourceValues(forKeys: [.fileSizeKey])
|
|
|
BFLog(message: "size = \(String(describing: size))")
|
|
|
resultHandler(playerItem, Float64(size?.fileSize ?? 0), info)
|
|
@@ -76,9 +76,9 @@ class PQPHAssetVideoParaseUtil: NSObject {
|
|
|
/// - asset: <#asset description#>
|
|
|
/// - resultHandler: <#resultHandler description#>
|
|
|
/// - Returns: <#description#>
|
|
|
- class func parasToAVAsset(phAsset: PHAsset,isHighQuality : Bool = true, resultHandler: @escaping (AVAsset?, Int, AVAudioMix?, [AnyHashable: Any]?) -> Void) {
|
|
|
+ class func parasToAVAsset(phAsset: PHAsset, isHighQuality: Bool = true, resultHandler: @escaping (AVAsset?, Int, AVAudioMix?, [AnyHashable: Any]?) -> Void) {
|
|
|
PHImageManager.default().requestAVAsset(forVideo: phAsset, options: videoRequestOptions) { avAsset, audioMix, info in
|
|
|
- if isHighQuality && (avAsset as? AVURLAsset)?.url.absoluteString.components(separatedBy: "/").last?.contains(".medium.") ?? false{
|
|
|
+ if isHighQuality, (avAsset as? AVURLAsset)?.url.absoluteString.components(separatedBy: "/").last?.contains(".medium.") ?? false {
|
|
|
let tempVideoOptions = PHVideoRequestOptions()
|
|
|
tempVideoOptions.version = .original
|
|
|
// 下载iCloud视频
|
|
@@ -92,7 +92,7 @@ class PQPHAssetVideoParaseUtil: NSObject {
|
|
|
BFLog(message: "size = \(String(describing: size))")
|
|
|
resultHandler(tempAvAsset, size?.fileSize ?? 0, tempAudioMix, tempInfo)
|
|
|
}
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
let size = try! (avAsset as? AVURLAsset)?.url.resourceValues(forKeys: [.fileSizeKey])
|
|
|
resultHandler(avAsset, size?.fileSize ?? 0, audioMix, info)
|
|
|
BFLog(message: "size = \(String(describing: size))")
|
|
@@ -106,7 +106,7 @@ class PQPHAssetVideoParaseUtil: NSObject {
|
|
|
/// - isAdjustRotationAngle: 是否调整旋转角度
|
|
|
/// - resultHandler: <#resultHandler description#>
|
|
|
/// - Returns: <#description#>
|
|
|
- class func exportPHAssetToMP4(phAsset: PHAsset, isAdjustRotationAngle: Bool = true, isCancelCurrentExport: Bool = false,deliveryMode: PHVideoRequestOptionsDeliveryMode? = .automatic, resultHandler: @escaping (_ phAsset: PHAsset, _ aVAsset: AVAsset?, _ filePath: String?, _ errorMsg: String?) -> Void) {
|
|
|
+ class func exportPHAssetToMP4(phAsset: PHAsset, isAdjustRotationAngle: Bool = true, isCancelCurrentExport: Bool = false, deliveryMode: PHVideoRequestOptionsDeliveryMode? = .automatic, resultHandler: @escaping (_ phAsset: PHAsset, _ aVAsset: AVAsset?, _ filePath: String?, _ errorMsg: String?) -> Void) {
|
|
|
BFLog(message: "导出相册视频-开始导出:phAsset = \(phAsset)")
|
|
|
if isCancelCurrentExport {
|
|
|
currentExportSession?.cancelExport()
|
|
@@ -116,7 +116,7 @@ class PQPHAssetVideoParaseUtil: NSObject {
|
|
|
// 创建目录
|
|
|
createDirectory(path: photoLibraryDirectory)
|
|
|
let fileName = (avAsset as! AVURLAsset).url.absoluteString
|
|
|
- let filePath = photoLibraryDirectory + (fileName.kf.md5).kf.md5 + ".mp4"
|
|
|
+ let filePath = photoLibraryDirectory + fileName.kf.md5.kf.md5 + ".mp4"
|
|
|
let data = try? Data(contentsOf: NSURL.fileURL(withPath: filePath))
|
|
|
if FileManager.default.fileExists(atPath: filePath) && (data?.count ?? 0) > fileSize / 40 {
|
|
|
BFLog(message: "导出相册视频-已经导出完成:\(filePath)")
|
|
@@ -148,7 +148,7 @@ class PQPHAssetVideoParaseUtil: NSObject {
|
|
|
BFLog(message: "导出相册视频-progress = \(progress),error = \(String(describing: error)),pointer = \(pointer),info = \(String(describing: info))")
|
|
|
}
|
|
|
requestOptions.deliveryMode = deliveryMode ?? .automatic
|
|
|
- PHImageManager.default().requestExportSession(forVideo: phAsset, options: requestOptions, exportPreset: (deliveryMode == .automatic || deliveryMode == .mediumQualityFormat) ? AVAssetExportPresetMediumQuality :(deliveryMode == .highQualityFormat ? AVAssetExportPresetHighestQuality : AVAssetExportPresetLowQuality) , resultHandler: { avAssetExportSession, _ in
|
|
|
+ PHImageManager.default().requestExportSession(forVideo: phAsset, options: requestOptions, exportPreset: (deliveryMode == .automatic || deliveryMode == .mediumQualityFormat) ? AVAssetExportPresetMediumQuality : (deliveryMode == .highQualityFormat ? AVAssetExportPresetHighestQuality : AVAssetExportPresetLowQuality), resultHandler: { avAssetExportSession, _ in
|
|
|
BFLog(message: "导出相册视频-请求到导出 avAssetExportSession = \(String(describing: avAssetExportSession))")
|
|
|
currentExportSession = avAssetExportSession
|
|
|
if avAssetExportSession != nil {
|
|
@@ -273,13 +273,14 @@ class PQPHAssetVideoParaseUtil: NSObject {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
/// PHAsset 转码为.mp4保存本地
|
|
|
/// - Parameters:
|
|
|
/// - phAsset: <#phAsset description#>
|
|
|
/// - isAdjustRotationAngle: 是否调整旋转角度
|
|
|
/// - resultHandler: <#resultHandler description#>
|
|
|
/// - Returns: <#description#>
|
|
|
- class func writePHAssetDataToMP4(phAsset: PHAsset, isAdjustRotationAngle: Bool = true, isCancelCurrentExport: Bool = false,deliveryMode: PHVideoRequestOptionsDeliveryMode? = .automatic, resultHandler: @escaping (_ phAsset: PHAsset, _ aVAsset: AVAsset?, _ filePath: String?, _ errorMsg: String?) -> Void) {
|
|
|
+ class func writePHAssetDataToMP4(phAsset: PHAsset, isAdjustRotationAngle _: Bool = true, isCancelCurrentExport: Bool = false, deliveryMode _: PHVideoRequestOptionsDeliveryMode? = .automatic, resultHandler: @escaping (_ phAsset: PHAsset, _ aVAsset: AVAsset?, _ filePath: String?, _ errorMsg: String?) -> Void) {
|
|
|
BFLog(message: "导出相册视频-开始导出:phAsset = \(phAsset)")
|
|
|
if isCancelCurrentExport {
|
|
|
currentExportSession?.cancelExport()
|
|
@@ -312,11 +313,11 @@ class PQPHAssetVideoParaseUtil: NSObject {
|
|
|
}
|
|
|
}
|
|
|
do {
|
|
|
- try FileManager.default.copyItem(at: (avAsset as! AVURLAsset).url, to: URL.init(fileURLWithPath: filePath))
|
|
|
+ try FileManager.default.copyItem(at: (avAsset as! AVURLAsset).url, to: URL(fileURLWithPath: filePath))
|
|
|
} catch {
|
|
|
BFLog(message: "导出相册视频-error == \(error)")
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// NSError *error;
|
|
|
// AVURLAsset *avurlasset = (AVURLAsset*)asset;
|
|
|
// NSURL *fileURL = [NSURL fileURLWithPath:savePath];
|
|
@@ -345,7 +346,7 @@ class PQPHAssetVideoParaseUtil: NSObject {
|
|
|
// BFLog(message: "导出相册视频-progress = \(progress),error = \(String(describing: error)),pointer = \(pointer),info = \(String(describing: info))")
|
|
|
// }
|
|
|
// requestOptions.deliveryMode = deliveryMode ?? .automatic
|
|
|
-
|
|
|
+
|
|
|
// PHImageManager.default().requestExportSession(forVideo: phAsset, options: requestOptions, exportPreset: (deliveryMode == .automatic || deliveryMode == .mediumQualityFormat) ? AVAssetExportPreset1920x1080 :(deliveryMode == .highQualityFormat ? AVAssetExportPresetHighestQuality : AVAssetExportPresetLowQuality) , resultHandler: { avAssetExportSession, _ in
|
|
|
// BFLog(message: "导出相册视频-请求到导出 avAssetExportSession = \(avAssetExportSession)")
|
|
|
// currentExportSession = avAssetExportSession
|
|
@@ -472,6 +473,7 @@ class PQPHAssetVideoParaseUtil: NSObject {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
/// 导出相册视频
|
|
|
/// - Parameters:
|
|
|
/// - aVAsset: <#aVAsset description#>
|
|
@@ -709,9 +711,10 @@ class PQPHAssetVideoParaseUtil: NSObject {
|
|
|
/// 获取gif帧跟时长
|
|
|
/// - Parameters:
|
|
|
/// - data: <#data description#>
|
|
|
+ /// - isRenderingTemplate
|
|
|
/// - resultHandler: <#resultHandler description#>
|
|
|
/// - Returns: <#description#>
|
|
|
- class func parasGIFImage(data: Data, resultHandler: @escaping (_ data: Data, _ images: [UIImage]?, _ duration: Double?) -> Void) {
|
|
|
+ class func parasGIFImage(data: Data, isRenderingColor: UIColor? = nil, resultHandler: @escaping (_ data: Data, _ images: [UIImage]?, _ duration: Double?) -> Void) {
|
|
|
let info: [String: Any] = [
|
|
|
kCGImageSourceShouldCache as String: true,
|
|
|
kCGImageSourceTypeIdentifierHint as String: kUTTypeGIF,
|
|
@@ -761,8 +764,13 @@ class PQPHAssetVideoParaseUtil: NSObject {
|
|
|
// 计算总时间
|
|
|
gifDuration += gifFrameDuration
|
|
|
// 2.图片
|
|
|
- let frameImage = UIImage(cgImage: imageRef, scale: 1.0, orientation: .up)
|
|
|
- images.append(frameImage)
|
|
|
+ var frameImage: UIImage? = UIImage(cgImage: imageRef, scale: 1.0, orientation: .up)
|
|
|
+ if isRenderingColor != nil {
|
|
|
+ frameImage = frameImage?.tintImage(color: isRenderingColor!, blendMode: .destinationIn)
|
|
|
+ }
|
|
|
+ if frameImage != nil {
|
|
|
+ images.append(frameImage!)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
resultHandler(data, images, gifDuration)
|