简体   繁体   中英

How to get a reference to the PHAsset from an image URL in the iOS Extension?

I'm working on an Apple Photos Extension. I need to get the photos HDR and Depth Effect flags from the PHAsset. The Extension only seems to give me a URL to a temp image file and not a reference to the PHAsset. In other words, I need to get the object of PHAsset from the Extension image URL (or some other way) so I can get the HDR and Depth Effect attributes.

for item: Any in self.extensionContext!.inputItems {
 let inputItem = item as! NSExtensionItem
 for provider: Any in inputItem.attachments! {
      let itemProvider = provider as! NSItemProvider
  if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeImage as String) { ......

Thank you so much,

Piyush

As far as I know depth (100%) and HDR (not sure :P) are in the Image Properties. So do this to get them:

let options = PHContentEditingInputRequestOptions()
//for icloud or itunes
options.isNetworkAccessAllowed = true

//self is the PHAsset Object here this snippet is from an extenstion I made.            
self.requestContentEditingInput(with: options, completionHandler: {
                (contentEditingInput, _) -> Void in
     if contentEditingInput != nil {
         if let url = contentEditingInput?.fullSizeImageURL {
         //URL is the path of the File in the 
             if let imageSource = CGImageSourceCreateWithURL(url, nil) {
                 if let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as Dictionary? {
                     //Now you can access the metadata here depth should be in exif afaik
                }
            }
        }
    }
}

the different Metadatas can be accessed through specific constants of Image I/O

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM