我们如何知道相册中是否已存在图像?

问题描述 投票:1回答:1

我已经以编程方式创建了相册。我想在添加新图像之前检查图像是否已经存在于相册中。

ios swift phasset phassetcollection
1个回答
0
投票
 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
        }

    }


}
© www.soinside.com 2019 - 2024. All rights reserved.