swiftphotosframework

Why the image URL for the same local image is different?


When I am selecting an image from UIImagePickerController and retiring file path by this code :

if let fileURL = info[.imageURL] as? NSURL {

}

The path for the image is "file:///private/var/mobile/Containers/Data/Application/3CE5CD49-4781-44B3-B021-77D745C97FDC/tmp/193E1106-30F0-4150-B230-7EC1B1EFB4B7.jpeg"

And when I am retiring file path for the same file with help of Photos framework with that code

asset.requestContentEditingInput(with: nil, completionHandler: { (editingInput, _) in
   editingInput?.fullSizeImageURL
})

The path for the image is "file:///var/mobile/Media/DCIM/108APPLE/IMG_8760.JPG"

So why image URLs for the same file are different?


Solution

  • iOS 8 onwards, Absolute url to app's sandbox changes every time you relaunch the app. Hence you should never save the absolute url of the video. Save the name of the video and recreate the url every time you relaunch the app.

    let pathComponent = "pack\(self.packID)-\(selectRow + 1).mp4"
      let directoryURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
      let folderPath: URL = directoryURL.appendingPathComponent("Downloads", isDirectory: true)
      let fileURL: URL = folderPath.appendingPathComponent(pathComponent)
    

    Once you have fileURL look for the file and you will find the file downloaded in previous launch.

    iOS creates a new Sandbox for app every time user launches the app. Hence absolute URL will very. But iOS will take care of setting up all the folders and contents inside the Sandbox as it was earlier. So though base url of SandBox change, relative url's of all the content will be remained intact.

    Hence its advised never to save absolute url to any folder :) Hope it helps