Bläddra i källkod

1.修改动图颜色

wenweiwei 3 år sedan
förälder
incheckning
f9278e0bda

+ 30 - 16
BFFramework/Classes/Categorys/UIImage+Ext.swift

@@ -8,17 +8,14 @@
 
 import Foundation
 
-extension UIImage {
-    
-    //从BFframwork bundle 中取图片
-    public func BF_Image(named:String) -> UIImage {
-  
-        let image:UIImage = UIImage.init(named: named, in:  Bundle.init().BF_mainbundle(), compatibleWith: nil) ?? UIImage.init()
+public extension UIImage {
+    // 从BFframwork bundle 中取图片
+    func BF_Image(named: String) -> UIImage {
+        let image: UIImage = UIImage(named: named, in: Bundle().BF_mainbundle(), compatibleWith: nil) ?? UIImage()
         return image
-        
     }
- 
-    public func cropImage(ratio: CGFloat) -> UIImage {
+
+    func cropImage(ratio: CGFloat) -> UIImage {
         // 计算最终尺寸
         let newSize: CGSize = CGSize(width: size.width, height: size.width * ratio)
         // 图片绘制区域
@@ -36,7 +33,7 @@ extension UIImage {
         return scaledImage!
     }
 
-    public func cropImage(newSize: CGSize) -> UIImage {
+    func cropImage(newSize: CGSize) -> UIImage {
         //// 图片绘制区域
         var rect = CGRect.zero
         rect.size.width = newSize.width
@@ -50,7 +47,7 @@ extension UIImage {
         return scaledImage!
     }
 
-    public func imageWithImage(scaledToSize newSize: CGSize) -> UIImage {
+    func imageWithImage(scaledToSize newSize: CGSize) -> UIImage {
         UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
         draw(in: CGRect(origin: CGPoint.zero, size: newSize))
         let newImage = UIGraphicsGetImageFromCurrentImageContext() ?? self
@@ -61,7 +58,7 @@ extension UIImage {
     /// 旋转角度
     /// - Parameter image: <#image description#>
     /// - Returns: <#description#>
-    public func rotateImage(rotate: Int, originWidth: CGFloat, originHeight: CGFloat) -> UIImage {
+    func rotateImage(rotate: Int, originWidth: CGFloat, originHeight: CGFloat) -> UIImage {
         let rotate: CGFloat = CGFloat(3 * Double.pi / 2)
         let rect = CGRect(x: 0, y: 0, width: originWidth, height: originHeight)
         let translateX: CGFloat = -rect.size.height
@@ -85,7 +82,7 @@ extension UIImage {
     ///   - tintColor: <#tintColor description#>
     ///   - convert:是否倒置
     /// - Returns: <#description#>
-    public class  func triangleImage(size: CGSize, tintColor: UIColor, direction: moveDirection = .moveDirectionDown) -> UIImage {
+    class func triangleImage(size: CGSize, tintColor: UIColor, direction: moveDirection = .moveDirectionDown) -> UIImage {
         var startPoint: CGPoint = CGPoint.zero
         var middlePoint: CGPoint = CGPoint.zero
         var endPoint: CGPoint = CGPoint.zero
@@ -123,7 +120,7 @@ extension UIImage {
 
     /// 按照最短边缩放  add by ak
     /// - Parameter maxLength: 边长最大值
-    public func nx_scaleWithMaxLength(maxLength: CGFloat) -> UIImage {
+    func nx_scaleWithMaxLength(maxLength: CGFloat) -> UIImage {
         if size.width > maxLength || size.height > maxLength {
             var maxWidth: CGFloat = maxLength
             var maxHeight: CGFloat = maxLength
@@ -149,7 +146,7 @@ extension UIImage {
 
     /// 缩放到指定大小 add by ak
     /// - Parameter size: 新的大小
-    public func nx_scaleToSize(size: CGSize) -> UIImage {
+    func nx_scaleToSize(size: CGSize) -> UIImage {
         var width: CGFloat = CGFloat(cgImage!.width)
         var height: CGFloat = CGFloat(cgImage!.height)
 
@@ -193,7 +190,7 @@ extension UIImage {
     }
 
     // 将图片裁剪成指定比例(多余部分自动删除)let image3 = image.crop(ratio: 1) /将图片转成 1:1 比例(正方形)
-    public func nxcrop(ratio: CGFloat) -> UIImage {
+    func nxcrop(ratio: CGFloat) -> UIImage {
         // 计算最终尺寸
         var newSize: CGSize!
         if size.width / size.height > ratio {
@@ -217,4 +214,21 @@ extension UIImage {
 
         return scaledImage!
     }
+    
+    /// 改变图片主题颜色
+    /// - Parameters:
+    ///   - color: <#color description#>
+    ///   - blendMode: <#blendMode description#>
+    /// - Returns: <#description#>
+    func tintImage(color: UIColor, blendMode: CGBlendMode) -> UIImage? {
+        let rect = CGRect(origin: CGPoint.zero, size: size)
+        UIGraphicsBeginImageContextWithOptions(size, false, scale)
+        color.setFill()
+        UIRectFill(rect)
+        draw(in: rect, blendMode: blendMode, alpha: 1.0)
+        let tintedImage = UIGraphicsGetImageFromCurrentImageContext()
+        UIGraphicsEndImageContext()
+        return tintedImage
+    }
 }
+

+ 14 - 4
BFFramework/Classes/Stuckpoint/View/PQStuckPointLoadingView.swift

@@ -13,10 +13,20 @@ class PQStuckPointLoadingView: UIView {
 
     var cancelHandle : ((_ sender: UIButton) -> Void)?
     /// 同步进度显示
-    lazy var loadingView: AnimatedImageView = {
-        let loadingView = AnimatedImageView()
-        loadingView.kf.setImage(with: URL(fileURLWithPath: Bundle.init().BF_mainbundle().path(forResource: "stuckPoint_edit_loading", ofType: ".gif")!))
-        loadingView.startAnimating()
+    lazy var loadingView: UIImageView = {
+        let loadingView = UIImageView()
+        loadingView.tintColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
+        let data = try? Data.init(contentsOf: URL(fileURLWithPath: Bundle.init().BF_mainbundle().path(forResource: "stuckPoint_edit_loading", ofType: ".gif")!))
+        if data != nil{
+            PQPHAssetVideoParaseUtil.parasGIFImage(data:data!,isRenderingColor: UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)) { data, images, duration in
+                loadingView.displayGIF(data: nil, images: images, repeatCount: .max, duration: duration ?? 2)
+            }
+        }
+        
+//        loadingView.kf.setImage(with: URL(fileURLWithPath: Bundle.init().BF_mainbundle().path(forResource: "stuckPoint_edit_loading", ofType: ".gif")!), completionHandler:  { image, error, type, url in
+//            loadingView.image = image?.withRenderingMode(.alwaysTemplate)
+//        })
+//        loadingView.startAnimating()
         return loadingView
     }()
 

+ 25 - 17
BFFramework/Classes/Utils/PQPHAssetVideoParaseUtil.swift

@@ -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)