Displaying images that were downloaded from Apple ODR

33 Views Asked by At

I have a set of images that I've added to the "cat-images" tag under Resource Tags in Xcode. In testing, I've done this:

let resourceRequest = NSBundleResourceRequest(tags: Set(["cat-images"]))
        resourceRequest.conditionallyBeginAccessingResources { (resourcesAvailable) in
            if resourcesAvailable {
                completion(nil)
            } else {
                resourceRequest.beginAccessingResources { (error) in
                    completion(error)
                }
            }
        }

This completes successfully, but then when I try do display one of the images from that set:

UIImage(named: "siamese.png").map { Image(uiImage: $0) }

The image cannot be found. Do I need to access the image in a different way if it's part of ODR? Does every single image in the folder need to have its own tag? Right now they're all tagged with "cat-images" but they don't have unique ODR tags.

I've also tried loading the image like this:

extension Image {
    static func loadFromODR(resourceName: String) -> Image? {
        let components = resourceName.split(separator: ".")
        let name = String(components[0])
        let ext = String(components[1])
        print("attempting odr image: \(name).\(ext)")
        guard let path = Bundle.main.path(forResource: name, ofType: ext),
              let image = UIImage(contentsOfFile: path) else {
            print("could not find: \(name).\(ext)")
            return nil
        }
        return Image(uiImage: image)
    }
}

but the image cannot be found.

0

There are 0 best solutions below