簡體   English   中英

從 UIImagePickerController 獲取深度數據

[英]Getting Depth Data from UIImagePickerController

我正在嘗試獲取與 PhotoLibrary 中的圖像相關聯的深度數據。 我可以獲得圖像和URL ,但我似乎無法獲得與之關聯的輔助數據。 調用CGImageSourceCreateWithURL返回一個源,但調用CGImageSourceCopyAuxiliaryDataInfoAtIndexkCGImageAuxiliaryDataTypeDisparitykCGImageAuxiliaryDataTypeDepth返回nil 有什么我在這里想念的嗎?

func imagePickerController(_ picker: UIImagePickerController,    didFinishPickingMediaWithInfo info: [String : Any]) {
    let image = info[UIImagePickerControllerOriginalImage]
    let url = info[UIImagePickerControllerImageURL]
    print("url=",url)

    guard let source = CGImageSourceCreateWithURL(url as! CFURL, nil) else {
        return
    }
    guard let auxDataInfo = CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypeDisparity) as? [AnyHashable : Any] else {
        return
    }
}

我為此掙扎了整整一天! 不過,在觀看了標題為“深度編輯圖像”的 WWDC 視頻的前半部分后,我終於明白了。

我的問題是使用不是來自 PHAsset 的圖像的 URL。

鏈接在這里:

WWDC 視頻鏈接

如果您不想觀看它,請查看我編寫的此功能,該功能與視頻中所做的幾乎完全相同。

您必須提供從 UIImagePickerDelegate 的 DID_FINISH_PICKING_IMAGE_WITH_INFO 函數返回的 [info] 函數。

在使用此功能之前 - 請注意這實際上不起作用! 不過很好看,因為它清楚地顯示了步驟。 但是由於異步行為,該函數在有機會將局部深度變量設置為 AVDepthData 之前將始終返回 nil。

我的解決方案是將函數分解並使用 Grand Central Dispatch 創建一個 Dispatch Group,輸入它,從 PHAsset 中檢索 imageURL,然后離開 Dispatch Group。 離開 Dispatch Group 后,DispatchGroup.NOTIFIED 函數繼續執行其余的過程。

我希望這有幫助!!!

     func returndepthdata(usingimageinfo: [UIImagePickerController.InfoKey : Any]) -> AVDepthData? {
          var depthdata: AVDepthData! = nil

          if let photoasset = usingimageinfo[.phAsset] as? PHAsset {
               let input = photoasset.requestContentEditingInput(with: nil, completionHandler: { (input, info) in
                    if let imageurl = input?.fullSizeImageURL {
                         if let source = CGImageSourceCreateWithURL(imageurl as CFURL, nil) {
                              if let imageproperties = CGImageSourceCopyProperties(source, nil) {
                                   if let disparityinfo = CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypeDisparity) {
                                        if let truedepthdata = try? AVDepthData(fromDictionaryRepresentation: disparityinfo as! [AnyHashable : Any]) {
                                             depthdata = truedepthdata
                                        }
                                   }
                              }
                         }
                    }
               })
          }
          return depthdata
     }

UIImagePickerController提供的圖像 URL 不包含任何與深度相關的元數據。 要獲取此信息,您必須使用 PhotoBook API 訪問PHAsset

首先,導入API:

import Photos

在顯示圖像選擇器之前,請求用戶訪問相冊。 您需要為照片庫使用添加一個信息字典鍵才能使其工作:

    switch PHPhotoLibrary.authorizationStatus() {
    case .notDetermined:
        PHPhotoLibrary.requestAuthorization { (status) in
            if status == .authorized {
                DispatchQueue.main.async {
                    // Display image picker here
                }
            }
        }
    case .authorized: // Display image picker here
    case .denied, .restricted: // Display appropriate error here
    }

現在,在您的圖像選擇器委托中,您可以執行以下操作:

    if let asset = info[.phAsset] as? PHAsset {
        PHImageManager.default().requestImageData(for: asset, options: nil) { (imageData, dataType, orientation, info) in
            let url = info?["PHImageFileURLKey"] as? URL
            // Pass this URL to your existing code.
        }
    }

請注意,該文件可能包含深度或視差信息。 您可以很容易地在這些之間進行轉換,但您可能需要使用CGImageSourceCopyProperties()檢查您擁有的是哪一個。 還要注意新的補充深度數據kCGImageAuxiliaryDataTypePortraitEffectsMatte ,它為肖像圖像中的主題人物提供了更高分辨率的蒙版,非常適合制作綠屏風格的效果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM