get Image from PHAsset issue to detect updates on images

452 views Asked by At

I'm currently managing a PHAsset with multiple Pictures (from an Album my application is creating), I'm able to display it without any problem. For each picture of my PHAsset, I'm calculating the hash of each pictures in my app. But If I'm modifying the picture out of my app (through the Photos, I'm changing the color of the picture for example) I'm not able to detect the update, if I'm calculating the hash one more time of the PHAsset pictures while my app is resumed, the update is not detected, the hashes remains the same...

Here is the code to get image from PHAsset:

var img: UIImage?
    let manager = PHImageManager.default()
    let options = PHImageRequestOptions()
    options.version = .original
    options.isSynchronous = true
    manager.requestImageData(for: asset, options: options) { (data, _, _, _) in
        if let data = data{
            img = UIImage(data: data)
        }

    }
    return img

I'm using this function while adding the picture (to calculate the hash), and when I need to check the hash of the PHasset's pictures, I'm retrieving the PHAssetCollection by using

        let collection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)

but no changes are detected...may be I need to ask for a refresh of the PHAssetCollection to retrieve the changes on the pictures ? Am I missing something ?

Just to be clear, I'm calculating the hash of the Image with the following:

//function described just above
let img = getSavedImageFromPHAsset(asset: myAsset)!
let img_hash = img.sha256()
//I'm comparing the hashes with this value

func sha256(data: Data) -> Data {
    var digestData = Data(count: Int(CC_SHA256_DIGEST_LENGTH))

    _ = digestData.withUnsafeMutableBytes {digestBytes in
        data.withUnsafeBytes {messageBytes in
            CC_SHA256(messageBytes, CC_LONG(data.count), digestBytes)
        }
    }
    return digestData
}

I assumed that as soon as an image linked to the PHAsset has been changed, I'm able to detect its modification (I can see the picture updated on my app's screen, but hash calculated is still the same)

1

There are 1 answers

0
holtmann On BEST ANSWER

Try the following:

options.version = .current

instead of

options.version = .original

Your code will always return the original, unedited photo/video. .current returns the same photo that you see in the iOS Photos App.