1

Problem: Loading an UIImage with an url obtained from PHAsset returns nil.

Context: I have a UI component which works with image URLs and is populated in different parts of the app with different providers. In a specific case I receive PHAsset.

Code for requesting PHAsset url:

        let inputRequestOptions = PHContentEditingInputRequestOptions()
        inputRequestOptions.isNetworkAccessAllowed = true

        asset.requestContentEditingInput(with: inputRequestOptions, completionHandler: { (edittingInput, _) in
            guard let url = edittingInput?.fullSizeImageURL else {
                return
            }

            switch asset.mediaType {
            case .image: editMediaContentContainerPresenter.assets = [.image(url)]
            case .video: editMediaContentContainerPresenter.assets = [.movie(url)]
            default:
                debugPrint("unsupported asset")
            }
        })

Code for loading the URL into an UIImage inside my UI component:

func image(for url: URL, maximumPixelSize: CGFloat) -> UIImage? {
    let options = [kCGImageSourceCreateThumbnailWithTransform: true,
                   kCGImageSourceCreateThumbnailFromImageAlways: true,
                   kCGImageSourceThumbnailMaxPixelSize: maximumPixelSize] as CFDictionary

    guard let source = CGImageSourceCreateWithURL(url as CFURL, nil) else {
        return nil
    }

    guard let imageReference = CGImageSourceCreateThumbnailAtIndex(source, 0, options) else {
        return nil
    }

    return UIImage(cgImage: imageReference, scale: UIScreen.main.scale, orientation: .up)
}

Special considerations: I need to transform the PHAsset into an URL because the current UI component is working with many other providers and parts of the app and all of them resolve to URLs. Sometimes the loading from that URL works, but usually maximum once. More often it doesn't manage to load an UIImage from the URL provided.

Question: How can I obtain a valid URL.

Solutions taken into consideration already, but to be avoided:

  1. loading the image with phasset requests and then saving it separately to a local url
  2. passing the UIImage instead of URL's
Fawkes
  • 3,831
  • 3
  • 22
  • 37
  • Only the Photos framework can access the image from a URL of a `PHAsset`. Look at the URL's scheme. It's a special scheme. – rmaddy Jun 11 '18 at 15:02
  • I also though so.. but sometimes I do manage to load it. Seems like that url is not made for any real usage outside the Photos framework... – Fawkes Jun 11 '18 at 15:11
  • You can fetch the URL data – Leo Dabus Jun 11 '18 at 15:25
  • @LeoDabus I did try, but as mentioned in the question it most often returns a nil UIImage. – Fawkes Jun 11 '18 at 15:33
  • Show how you are trying to fetch the assets data using the Photos framework – Leo Dabus Jun 11 '18 at 15:34
  • This might help https://stackoverflow.com/questions/43943629/swift-photo-library-access/43944769#43944769 – Leo Dabus Jun 11 '18 at 15:35
  • @LeoDabus so, look above in the code. First I fetch the image url through photos framework provided methods and then I try to load the image from that URL not through Photos framework. – Fawkes Jun 11 '18 at 15:48

0 Answers0