|
@@ -40,15 +40,62 @@ class TSUserdPhotoDiskTool {
|
|
|
|
|
|
import Foundation
|
|
|
import CommonCrypto
|
|
|
+import CoreImage
|
|
|
|
|
|
extension UIImage {
|
|
|
func uniqueIdentifier() -> String? {
|
|
|
+ dePrint("uniqueIdentifier 1")
|
|
|
guard let imageData = self.jpegData(compressionQuality: 0.1) else { return nil }
|
|
|
|
|
|
var digest = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
|
|
|
imageData.withUnsafeBytes {
|
|
|
_ = CC_SHA256($0.baseAddress, CC_LONG(imageData.count), &digest)
|
|
|
}
|
|
|
- return digest.map { String(format: "%02hhx", $0) }.joined()
|
|
|
+ let string = digest.map { String(format: "%02hhx", $0) }.joined()
|
|
|
+ dePrint("uniqueIdentifier 2")
|
|
|
+ return string
|
|
|
}
|
|
|
+
|
|
|
+// func uniqueIdentifier() -> String? {
|
|
|
+// dePrint("uniqueIdentifier 1")
|
|
|
+// guard let ciImage = CIImage(image: self) else { return nil }
|
|
|
+// let filter = CIFilter(name: "CIAreaAverage", parameters: [kCIInputImageKey: ciImage])
|
|
|
+// guard let outputImage = filter?.outputImage else { return nil }
|
|
|
+//
|
|
|
+// var bitmap = [UInt8](repeating: 0, count: 4)
|
|
|
+// let context = CIContext(options: [.workingColorSpace: kCFNull!])
|
|
|
+// context.render(outputImage,
|
|
|
+// toBitmap: &bitmap,
|
|
|
+// rowBytes: 4,
|
|
|
+// bounds: CGRect(x: 0, y: 0, width: 1, height: 1),
|
|
|
+// format: .RGBA8,
|
|
|
+// colorSpace: nil)
|
|
|
+// let string = bitmap.map { String(format: "%02hhx", $0) }.joined()
|
|
|
+// dePrint("uniqueIdentifier 2")
|
|
|
+// return string
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// func uniqueIdentifier() -> String? {
|
|
|
+// dePrint("uniqueIdentifier 1")
|
|
|
+// guard let ciImage = CIImage(image: self) else { return nil }
|
|
|
+//
|
|
|
+// // 使用CIFeature检测关键点(比计算整个图像哈希快)
|
|
|
+// let options = [CIDetectorAccuracy: CIDetectorAccuracyLow]
|
|
|
+// let detector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: options)
|
|
|
+// let features = detector?.features(in: ciImage)
|
|
|
+//
|
|
|
+// // 使用特征点位置生成简化的哈希
|
|
|
+// let featureData = features?.map { "\($0.bounds.origin.x)_\($0.bounds.origin.y)" }.joined()
|
|
|
+// let string = featureData?.data(using: .utf8)?.md5Hash()
|
|
|
+// dePrint("uniqueIdentifier 2")
|
|
|
+// return string
|
|
|
+// }
|
|
|
}
|
|
|
+//extension Data {
|
|
|
+// func md5Hash() -> String {
|
|
|
+// var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
|
|
|
+// _ = self.withUnsafeBytes { CC_MD5($0.baseAddress, CC_LONG(self.count), &digest) }
|
|
|
+// return digest.map { String(format: "%02hhx", $0) }.joined()
|
|
|
+// }
|
|
|
+//}
|