【问题标题】:How can we know if image already exists in the album?我们如何知道相册中是否已经存在图像?
【发布时间】:2020-04-04 17:50:05
【问题描述】:

我以编程方式创建了一个相册。我想在添加新图像之前检查相册中是否已经存在图像。 你可以用下面的代码检查 CustomAlbum.shared.saveImage(图像: UIImage()) 像这样保存图像。

import Foundation
import Photos
class CustomAlbum {
    static let albumName = "xyz"
    static let shared = CustomAlbum()
    var assetCollection: PHAssetCollection!
    init() {
        func fetchAssetCollectionForAlbum() -> PHAssetCollection! {
            let fetchOptions = PHFetchOptions()
            fetchOptions.predicate = NSPredicate(format: "title = %@", CustomAlbum.albumName)
            let collection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
            if let _: AnyObject = collection.firstObject {
                return collection.firstObject!
            }
            return nil
        }
        if let assetCollection = fetchAssetCollectionForAlbum() {
            self.assetCollection = assetCollection
            return
        }
        PHPhotoLibrary.shared().performChanges({
            PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: CustomAlbum.albumName)
        }) { _, _ in
            self.assetCollection = fetchAssetCollectionForAlbum()
        }
    }

    func saveImage(image: UIImage) {
        if assetCollection == nil { return }
        PHPhotoLibrary.shared().performChanges({
            let assetPlaceholder = PHAssetChangeRequest.creationRequestForAsset(from: image).placeholderForCreatedAsset
            let albumChangeRequest = PHAssetCollectionChangeRequest(for: self.assetCollection)
            let assetEnumeration: NSArray = [assetPlaceholder!]
            albumChangeRequest?.addAssets(assetEnumeration)
        }, completionHandler: nil)
    }
    func allPhotos(albumName: String = "xyz", albumImages: @escaping ([UIImage]) -> Void) {
        let group = DispatchGroup()
        var images: [Image] = []
        let fetchOptions = PHFetchOptions()
        fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)

        let resultCollections = PHAssetCollection.fetchAssetCollections(
            with: .album,
            subtype: .albumRegular,
            options: fetchOptions)

        resultCollections.enumerateObjects({
            (object, index, stop) -> Void in
            let collection = object
            let result = PHAsset.fetchAssets(in: collection, options: nil)
            result.enumerateObjects({
                (object, index, stop) -> Void in
                group.enter()
                images.append(object.getfullImage())
                group.leave()
            })
        })
        group.notify(queue: DispatchQueue.main) {
            albumImages(images)
        }
    }
}

使用此代码,我正在获取相册的所有图像并与我要保存的图像进行比较。

    PHPhotoLibrary.shared().allPhotos { (images) in 
    let image = UIImage()

}

【问题讨论】:

  • 分享你到目前为止所尝试的。
  • 请添加一些代码
  • 你想如何根据名字检查它是否存在?
  • 我们可以根据 UIImage() 对象进行比较吗?或者我们可以得到我们保存在相册中的准确图像吗?我们可以从 PHAsset 获取图像。

标签: ios swift phasset phassetcollection


【解决方案1】:
 class func getPhotofolder() -> String{
    let fileManager = FileManager.default
    let paths = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("hsafetyPhoto") //hsafetyPhoto is my folder name to store the image  and if doesnt exist then system will create one 
    if !fileManager.fileExists(atPath: paths){
        try! fileManager.createDirectory(atPath: paths, withIntermediateDirectories: true, attributes: nil)

    }else{
        print("Already dictionary created.")
    }

    return  paths
}


//Save Image At hsafetyPhoto Document Directory
class func saveImageDocumentDirectory(photo : UIImage, photoUrl : String) -> Bool{

    let fileManager = FileManager.default

    let paths =  Utility.getPhotofolder().stringByAppendingPathComponent(pathComponent: photoUrl)
    print("image's path \(paths)")
    if !fileManager.fileExists(atPath: paths){

        let imageData = UIImageJPEGRepresentation(photo, 0.5)
        fileManager.createFile(atPath: paths as String, contents: imageData, attributes: nil)

        if !fileManager.fileExists(atPath: paths){
            return false
        }else{
            return true
        }

    }else{
        print(paths)
        let imageData = UIImageJPEGRepresentation(photo, 0.5)
        fileManager.createFile(atPath: paths as String, contents: imageData, attributes: nil)
        if !fileManager.fileExists(atPath: paths){
            return false
        }else{
            return true
        }

    }


}

【讨论】:

  • 谢谢,但我不是在谈论将图像保存在文档目录中。如果我删除该应用程序,我保存在文档目录中的所有图像都将被删除。我所做的是将图像直接保存在我创建的相册下的照片中。
猜你喜欢
  • 2016-09-23
  • 1970-01-01
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多