Check if image exists in Photo

I have an image album controlled by a remote server. I would like to give the user the opportunity to upload an album and save it in a user album in Photos. But since the album is dynamic (photos are added to it), the user can download it several times. I do not want to upload the same images several times, only new ones.

Can I link some metadata (unique identifier) ​​when I save the image in the Photos application? And then check if this image exists?

I use the Photos Framework to create a custom album and save photos.

Edit: Here is my code for creating a custom album and saving photos

/** Returns the first album from the photos app with the specified name. */ static func getAlbumWithName(name: String, completion: (album: PHAssetCollection?) -> Void) { let fetchOptions = PHFetchOptions() fetchOptions.predicate = NSPredicate(format: "localizedTitle = %@", name) let fetchResult = PHAssetCollection.fetchAssetCollectionsWithType(PHAssetCollectionType.Album, subtype: PHAssetCollectionSubtype.Any, options: fetchOptions) if fetchResult.count > 0 { guard let album = fetchResult.firstObject as? PHAssetCollection else {return} completion(album: album) } else { PHPhotoLibrary.sharedPhotoLibrary().performChanges({ PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(name) }, completionHandler: { (result, error) in if result { FileUtils.getAlbumWithName(name, completion: completion) } else { completion(album: nil) } }) } } /** Adds an image to the specified photos app album */ private static func addImage(image: UIImage, toAlbum album: PHAssetCollection, completion: ((status: Bool) -> Void)?) { PHPhotoLibrary.sharedPhotoLibrary().performChanges({ let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image) let assetPlaceholder = assetRequest.placeholderForCreatedAsset let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: album) albumChangeRequest?.addAssets([assetPlaceholder!]) }) { (status, error) in completion?(status: status) } } 
+6
source share
1 answer

All you have to do is read the "localIdentifier" from the asset placeholder. I added code to return the id to the completion handler. You can deal with these options.

 private static func addImage(image: UIImage, toAlbum album: PHAssetCollection, completion: ((status: Bool, identifier: String?) -> Void)?) { var localIdentifier: String? PHPhotoLibrary.sharedPhotoLibrary().performChanges({ let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image) let assetPlaceholder = assetRequest.placeholderForCreatedAsset let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: album) albumChangeRequest?.addAssets([assetPlaceholder!]) localIdentifier = assetPlaceholder?.localIdentifier }) { (status, error) in completion?(status: status, identifier: localIdentifier) } } 

If you want to read this asset again, your image image method might look something like this (I have not used your legend or variable names). This will read the asset synchronously, but I'm sure you can define an asynchronous option.

 internal func loadPhoto(identifier: String) -> UIImage? { if assetCollection == nil { return nil } let fetchOptions = PHFetchOptions() fetchOptions.predicate = NSPredicate(format: "localIdentifier = %@", identifier) let fetchResult = PHAsset.fetchAssetsInAssetCollection(assetCollection, options: fetchOptions) if fetchResult.count > 0 { if let asset = fetchResult.firstObject as? PHAsset { let options = PHImageRequestOptions() options.deliveryMode = .HighQualityFormat options.synchronous = true var result: UIImage? PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: CGSize(width: asset.pixelWidth, height: asset.pixelHeight), contentMode: .AspectFit, options: options, resultHandler: {(image: UIImage?, _: [NSObject: AnyObject]?) -> Void in result = image }) return result } } return nil } 
+1
source

All Articles