简体   繁体   English

重启iOS应用后检查相机胶卷中照片是否仍然存在

[英]Check if photo still exists in camera roll after restart ios app

I'm still new in iOS development and I'm creating an app that allows to select some photos and access them quickly. 我仍然是iOS开发的新手,并且正在创建一个允许选择一些照片并快速访问它们的应用程序。 In CoreData, I store the referenceUrl of the photo instead of the photo itself. 在CoreData中,我存储照片的referenceUrl而不是照片本身。 I'm able to load photos when starting the app this way. 以这种方式启动应用程序时,我可以加载照片。

But, before loading photos, I need to check that they still exist and cannot figure out how to check, using the referenceUrl, if a photo exists or not. 但是,在加载照片之前,我需要检查它们是否仍然存在,并且无法使用referenceUrl找出如何检查照片是否存在。

Even using the localPath does not work. 即使使用localPath也不起作用。 It returns false all the time. 它始终返回false。

If you have any idea how I can proceed, I'll be very grateful. 如果您有任何想法,我将不胜感激。 Thanks. 谢谢。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        let imageUrl          = info[UIImagePickerControllerReferenceURL] as! NSURL
        let imageName         = imageUrl.lastPathComponent
        let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
        let photoURL          = NSURL(fileURLWithPath: documentDirectory)
        let localPath         = photoURL.appendingPathComponent(imageName!)!
        let localPathString   = String(describing: localPath)

        let fileManager = FileManager.default
        if fileManager.fileExists(atPath: localPathString){
            print ("Photo exists")
        } else{
            print ("Photo does not exist")
        }
}

I found a solution, here it is. 我找到了一个解决方案,就在这里。

I use imageUrl from my code above like that: 我从上面的代码中使用imageUrl

let assets = PHAsset.fetchAssets(withALAssetURLs: [imageUrl as URL], options: nil)
if assets.firstObject != nil {
   //Then the image is still there
}
else{
   //The image is not present anymore
}

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM