2017-04-13 92 views
0

后,我仍然在iOS开发新的,我创建一个应用程序,允许选择一些照片和快速访问它们。 在CoreData,我存储的照片,而不是照片本身的referenceUrl。我可以通过这种方式启动应用程序时加载照片。检查照片在相机胶卷仍然存在重启iOS应用

但是,在加载照片之前,我需要检查,他们仍然存在,无法弄清楚如何检查,使用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") 
     } 
} 

回答

0

我发现了一个解决方案,在这里。

我用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 
} 

希望有所帮助。

相关问题