I try to get fullSizeImageURL and then calculate md5 of asset via url:
var assetUrl = syncUrl(asset: asset)
let assetMD5 = md5File(url: assetUrl)
To get url I use requestContentEditingInput. The problem is contentEditingInput can be empty. I have had cases where my app worked on my new iphone the first few times. But after some tries, this problem was gone and contentEditingInput was always not empty. Not sure but I think it is because all assets were cached.
So my questions are:
- Why contentEditingInput can be empty?
- How can to get asset url if contentEditingInput is empty?
- How can I clear asset photos cache to reproduce this issue if it is related to cache?
Part of code which I use:
extension PHAsset {
func getURL(completionHandler: @escaping ((_ responseURL: URL?) -> Void)) {
let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
options.canHandleAdjustmentData = { (adjustmeta: PHAdjustmentData) -> Bool in
return true
}
options.isNetworkAccessAllowed = true
self.requestContentEditingInput(with: options, completionHandler: { (contentEditingInput: PHContentEditingInput?, info: [AnyHashable: Any]) -> Void in
!!!!!!contentEditingInput can be empty
completionHandler(contentEditingInput?.fullSizeImageURL as URL?)
})
}
md5:
func md5File(url: URL, isCancelled: @escaping () -> Bool) -> Data? {
let bufferSize = 1024 * 1024
do {
// Open file for reading:
let file = try FileHandle(forReadingFrom: url)
defer {
file.closeFile()
}
// Create and initialize MD5 context:
var context = CC_MD5_CTX()
CC_MD5_Init(&context)
// Read up to `bufferSize` bytes, until EOF is reached, and update MD5 context:
while autoreleasepool(invoking: {
if isCancelled() { return false }
let data = file.readData(ofLength: bufferSize)
if data.count > 0 {
data.withUnsafeBytes {
_ = CC_MD5_Update(&context, $0.baseAddress, numericCast(data.count))
}
return true // Continue
} else {
return false // End of file
}
}) { }
if isCancelled() { return nil }
// Compute the MD5 digest:
var digest: [UInt8] = Array(repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
_ = CC_MD5_Final(&digest, &context)
return Data(digest)
} catch {
print("Cannot open file:", error.localizedDescription)
return nil
}
}