[英]Check if photo still exists in camera roll after restart ios app
我仍然是iOS开发的新手,并且正在创建一个允许选择一些照片并快速访问它们的应用程序。 在CoreData中,我存储照片的referenceUrl而不是照片本身。 以这种方式启动应用程序时,我可以加载照片。
但是,在加载照片之前,我需要检查它们是否仍然存在,并且无法使用referenceUrl找出如何检查照片是否存在。
即使使用localPath也不起作用。 它始终返回false。
如果您有任何想法,我将不胜感激。 谢谢。
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")
}
}
我找到了一个解决方案,就在这里。
我从上面的代码中使用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
}
希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.