Skip to content

Instantly share code, notes, and snippets.

@eleev
Last active April 2, 2025 13:33
Show Gist options
  • Select an option

  • Save eleev/2f041bb8b1936093773f0bde42af3a49 to your computer and use it in GitHub Desktop.

Select an option

Save eleev/2f041bb8b1936093773f0bde42af3a49 to your computer and use it in GitHub Desktop.
Getting URL for PHAsset (Swift 3.0)
func getURL(ofPhotoWith mPhasset: PHAsset, completionHandler : @escaping ((_ responseURL : URL?) -> Void)) {
if mPhasset.mediaType == .image {
let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
return true
}
mPhasset.requestContentEditingInput(with: options, completionHandler: { (contentEditingInput, info) in
completionHandler(contentEditingInput!.fullSizeImageURL)
})
} else if mPhasset.mediaType == .video {
let options: PHVideoRequestOptions = PHVideoRequestOptions()
options.version = .original
PHImageManager.default().requestAVAsset(forVideo: mPhasset, options: options, resultHandler: { (asset, audioMix, info) in
if let urlAsset = asset as? AVURLAsset {
let localVideoUrl = urlAsset.url
completionHandler(localVideoUrl)
} else {
completionHandler(nil)
}
})
}
}
@RamohanReddy

RamohanReddy commented Mar 27, 2019

Copy link
Copy Markdown

Hi @jVirus
I am using below code some times getting asset values some times getting nil.
what is the issue here? how can i do ?

  PHImageManager.default().requestAVAsset(forVideo: mPhasset, options: options, resultHandler: { (asset, audioMix, info) in
                if let urlAsset = asset as? AVURLAsset {
                    let localVideoUrl = urlAsset.url
                    completionHandler(localVideoUrl)
                } else {
                    completionHandler(nil)
                }
            })

Thanks&Regards
Ramohan reddy

@ptvyas

ptvyas commented Feb 12, 2020

Copy link
Copy Markdown

Hi @jVirus
I have not got URL in the iPhone X device
My code is,

if mPhasset.mediaType == .image {
                let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions.init()
                options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
                    return true
                }
                mPhasset.requestContentEditingInput(with: options, completionHandler: { (contentEditingInput, info) in
                    //completionHandler(contentEditingInput!.fullSizeImageURL)
                    if let value = contentEditingInput {
                        completionHandler(value.fullSizeImageURL)
                    }
                })
            } else if mPhasset.mediaType == .video {
                let options: PHVideoRequestOptions = PHVideoRequestOptions()
                options.version = .original
                PHImageManager.default().requestAVAsset(forVideo: mPhasset, options: options, resultHandler: { (asset, audioMix, info) in
                    if let urlAsset = asset as? AVURLAsset {
                        let localVideoUrl = urlAsset.url
                        completionHandler(localVideoUrl)
                    } else {
                        completionHandler(nil)
                    }
                })
            } 

Any idea ??

@ptvyas

ptvyas commented Feb 12, 2020

Copy link
Copy Markdown

I have get the contentEditingInput nil value in Image and
asset value is nil form video asses

@sravyagajavalli

Copy link
Copy Markdown

I am also having the same issue. It is not getting in the iPhone X device how should I solve it. -@ptvyas

@eleev

eleev commented Feb 18, 2020

Copy link
Copy Markdown
Author

Hello, @RamohanReddy, @ptvyas, @sravyagajavalli!

As the description of the gist mentions, it was originally developed and tested alongside the Swift 3.0 and the corresponding iOS SDK. Since then, a lot of things have changed. I can't tell you immediately what's wrong with the snippet for the iPhone X and iOS 13.0 += 1 version.

I will later check it out and post an update.

@freemansion

freemansion commented Jun 19, 2020

Copy link
Copy Markdown

thanks for sharing the snippet.
I would just add - in order to fetch videos stored in iCloud (otherwise result handler will return nil) - set PHVideoRequestOptions's param isNetworkAccessAllowed to be true.

@eleev

eleev commented Jun 22, 2020

Copy link
Copy Markdown
Author

@freemansion greatly appreciate for pointing that important case out.

@fernandodev

Copy link
Copy Markdown

Thanks! I slightly changed it for what i need

    func requestAssetUrl() -> AnyPublisher<URL, Error> {
        Future { [self] promise in
            if self.mediaType == .image {
                self.requestContentEditingInput(with: nil) { input, info in
                    if let input = input, let url = input.fullSizeImageURL {
                        promise(.success(url))
                    } else {
                        promise(.failure(Errors.urlNotAvailable))
                    }
                }
            } else if self.mediaType == .video {
                let options: PHVideoRequestOptions = PHVideoRequestOptions()
                options.version = .original
                PHImageManager.default().requestAVAsset(forVideo: self, options: options) { asset, audio, info in
                    if let urlAsset = asset as? AVURLAsset {
                        let localVideoUrl = urlAsset.url
                        promise(.success(localVideoUrl))
                    } else {
                        promise(.failure(Errors.urlNotAvailable))
                    }
                }
            } else {
                promise(.failure(Errors.mediaNotSupported))
            }
        }.eraseToAnyPublisher()
    }
    
    extension PHAsset {
      enum Errors: Error {
          case urlNotAvailable
          case mediaNotSupported
      }
    }

@FreakyAli

Copy link
Copy Markdown

@fernandodev I recommend you add some code that shows how to use this piece. Newbies would need it, but that definitely helped. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment