2

Had to repost this question because no one answered, it's been 2 weeks, and I am absolutely clueless on why this is happening. I am using the following function to convert a PHAsset to a url:

extension PHAsset {

    func getURL(completionHandler : @escaping ((_ responseURL : URL?) -> Void)){
        if self.mediaType == .image {
            let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
            options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
                return true
            }
            options.isNetworkAccessAllowed = true
            self.requestContentEditingInput(with: options, completionHandler: {(contentEditingInput: PHContentEditingInput?, info: [AnyHashable : Any]) -> Void in
                completionHandler(contentEditingInput!.fullSizeImageURL as URL?)
            })
        } else if self.mediaType == .video {
            let options: PHVideoRequestOptions = PHVideoRequestOptions()
            options.version = .original
            options.isNetworkAccessAllowed = true
            PHImageManager.default().requestAVAsset(forVideo: self, options: options, resultHandler: {(asset: AVAsset?, audioMix: AVAudioMix?, info: [AnyHashable : Any]?) -> Void in
                if let urlAsset = asset as? AVURLAsset {
                    let localVideoUrl: URL = urlAsset.url as URL
                    completionHandler(localVideoUrl)
                } else {
                    completionHandler(nil)
                }
            })
        }
    }
}

However the url ends up coming out nil sometimes causing my app to crash. I have noticed it is more common if I choose a video on the longer side rather than a picture but I can not pin point exactly when it happens. Also, this does not seem like an issue just I am having because on the stack overflow post I got this from, the recent comment asks why the asset is sometimes nil. The post: How to get URL for a PHAsset? [duplicate]

Edit

I changed code to this:

func getURL(completionHandler : @escaping ((_ responseURL : URL?) -> Void)){
        if self.mediaType == .image {
            let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
          //  options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
          //      return true
         //   }
           // options.isNetworkAccessAllowed = true
            self.requestContentEditingInput(with: options, completionHandler: {(contentEditingInput: PHContentEditingInput?, info: [AnyHashable : Any]) -> Void in
                if let contentEditingInput = contentEditingInput {
                   completionHandler(contentEditingInput.fullSizeImageURL)
                } else {
                   completionHandler(nil)
                }
            })
        } else if self.mediaType == .video {
            let options: PHVideoRequestOptions = PHVideoRequestOptions()
         //   options.version = .original
        //    options.isNetworkAccessAllowed = true
            PHImageManager.default().requestAVAsset(forVideo: self, options: options, resultHandler: {(asset: AVAsset?, audioMix: AVAudioMix?, info: [AnyHashable : Any]?) -> Void in
                if let urlAsset = asset as? AVURLAsset {
                    let localVideoUrl: URL = urlAsset.url as URL
                    completionHandler(localVideoUrl)
                } else {
                    completionHandler(nil)
                }
            })
        }
    }

But still no luck. My app is not crashing its just every now and then the urls come out to be nil.

coder12345
  • 69
  • 1
  • 8

1 Answers1

0
self.requestContentEditingInput(with: options, completionHandler: {(contentEditingInput: PHContentEditingInput?, info: [AnyHashable : Any]) -> Void in
                completionHandler(contentEditingInput!.fullSizeImageURL as URL?)
            })

contentEditingInput is marked as optional, why would anyone implicitly unwrap it is beyond me. To safely unwrap it (which will prevent your app from crashing), do something like this:

if let contentEditingInput = contentEditingInput {
   completionHandler(contentEditingInput.fullSizeImageURL)
} else {
   completionHandler(nil)
}

You do not need as URL? because, documentation:

var fullSizeImageURL: URL? { get }

Trying commenting out the line where you set the image options. I have an app that uses this API working fine, but I do not have those options set. This option could be the cause why contentEditingInput is nil.

Edit - getting URL of image or movie from Photo Library using UIImagePickerController

import MobileCoreServices

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard let mediaType = info[UIImagePickerController.InfoKey.mediaType] as? String,
              mediaType == (kUTTypeMovie as String) else { return }
        let videoURL = info[.mediaURL] as? URL
    }

Above is the way I used to get videoURL, below is for image.

guard let mediaType = info[UIImagePickerController.InfoKey.mediaType] as? String,
                  mediaType == (kUTTypeImage as String) else { return }
let photoURL = info[.imageURL] as? URL
cora
  • 1,916
  • 1
  • 10
  • 18
  • I just updated my answer to show you what code I tried but still no luck. If you still have your code would you be able to show it maybe because I am basically hopeless right now. – coder12345 Dec 08 '20 at 03:22
  • Can also include the code for the closure? The url is nil only for videos sometimes? – cora Dec 08 '20 at 03:30
  • Sorry to be "that guy" but what do you mean the code for the closure and yes, sometimes when I select a video it comes out nil and same for photos. Thats the frustrating part it happens about 1/3 times so I really cannot pinpoint what the problem is. – coder12345 Dec 08 '20 at 03:38
  • I am using a simple image picker and when I pick an image or video I do asset.getURL(). The asset is just the ph asset of either the image or video – coder12345 Dec 08 '20 at 03:52
  • I updated my answer. Next time give the whole story, please. – cora Dec 08 '20 at 04:10
  • I am still getting these urls to be nil sometimes really not sure what the issue is – coder12345 Dec 08 '20 at 22:07
  • Okkkkkk, so I finally kind of figured out the problem. It never happens the first time I run the code but if I close the view controller and select another image or video it comes out nil. – coder12345 Dec 08 '20 at 22:25
  • I got it!!!!!!!! Thank you for your help and input, it turned out being that for whatever reason hidden at the bottom of all my code on a different view controller where I declared them to be nil. – coder12345 Dec 08 '20 at 22:29