Browse Source

添加图片保存到沙盒方法

jsonwang 3 years ago
parent
commit
4dc457b00b
1 changed files with 21 additions and 0 deletions
  1. 21 0
      BFFramework/Classes/BFModules/BFCategorys/BFUIImage+Ext.swift

+ 21 - 0
BFFramework/Classes/BFModules/BFCategorys/BFUIImage+Ext.swift

@@ -230,5 +230,26 @@ public extension UIImage {
         UIGraphicsEndImageContext()
         return tintedImage
     }
+    
+    /// 保存图片文件到指定目录, 如果目录已经存在会先删除老文件
+    /// - Parameters:
+    ///   - currentImage: 图片数据
+    ///   - persent: 质量
+    ///   - outFilePath: 输出目录
+    class func saveImage(currentImage: UIImage,outFilePath: String) {
+        // 文件存在先删除老文件
+        if FileManager.default.fileExists(atPath: outFilePath) {
+            do {
+                try FileManager.default.removeItem(at: NSURL.fileURL(withPath: outFilePath))
+            } catch {
+                BFLog(message: "删除文件出错 == \(error) \(outFilePath)")
+            }
+        }
+        
+        if let imageData = currentImage.pngData() {
+            try? imageData.write(to: URL(fileURLWithPath: outFilePath))
+            print("保存图片成功到:filePath=\(outFilePath)")
+        }
+    }
 }