Alper
Alper

Reputation: 1445

How to get duplicated medias in Photo Library?

I want to find duplicated photos/videos in photo library and delete them. But I got confused how can I compare all photos to each other and find which of them are duplicated.

Comparing bytes is not useful, which way should I take?

Upvotes: 0

Views: 1166

Answers (2)

adamfowlerphoto
adamfowlerphoto

Reputation: 2751

Using the Photos framework you can get a list of all media using the following

PHPhotoLibrary.requestAuthorization { status in
    if status == .authorized {
        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending:true)]
        assets = PHAsset.fetchAssets(with: fetchOptions)
    }
}

The code above orders the assets by creation date. That means when checking through your list of 5000 odd assets you only need to check neighboring assets in the list to see if they are duplicates.

Upvotes: 1

iLion
iLion

Reputation: 106

You can use below line of code to get properties of media and you can compare metadata with other media files. By this way you can check for duplicate media.

let metadata = info[UIImagePickerControllerMediaMetadata] as? NSDictionary    

Upvotes: 2

Related Questions