【问题标题】:Getting url for PHAsset获取 PHAsset 的 url
【发布时间】:2016-02-24 19:19:45
【问题描述】:

我正在使用自定义图像选择器来选择时间流逝,我想获取所选择时间流逝的 url,这样我就可以播放时间流逝了。

- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingAssets:(NSArray *)assets {
for (PHAsset *asset in assets) {



    NSURL *url = [NSURL URLWithString: assetURLgoesHere];
    AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:avAsset];
    AVPlayer *videoPlayer = [AVPlayer playerWithPlayerItem:playerItem];


    [videoPlayer play];

}

[self dismissViewControllerAnimated:YES completion:NULL];
}

这是我目前拥有的,但我需要获取资产的 url。

我通常使用 swift,但这个自定义图像选择器是用 Objective-c 编写的。所以我有点客观-c 菜鸟

更新代码

- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingAssets:(NSArray *)assets {
for (PHAsset *asset in assets) {

    - (PHImageRequestID)requestAVAssetForVideo:(PHAsset *)asset options:(PHVideoRequestOptions *)options resultHandler:(void (^)(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info))resultHandler

    NSURL *url = [NSURL URLWithString: asset];
    AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:avAsset];
    AVPlayer *videoPlayer = [AVPlayer playerWithPlayerItem:playerItem];


    [videoPlayer play];

}

[self dismissViewControllerAnimated:YES completion:NULL];
}

【问题讨论】:

    标签: ios objective-c avplayer avkit


    【解决方案1】:

    @HirenPanchal 在 Swift 4.0(也适用于 Swift 5.0+)和 iOS 9.0+ 中的正确答案

    func getUrlFromPHAsset(asset: PHAsset, callBack: @escaping (_ url: URL?) -> Void)
    {
        asset.requestContentEditingInput(with: PHContentEditingInputRequestOptions(), completionHandler: { (contentEditingInput, dictInfo) in
    
            if let strURL = (contentEditingInput!.audiovisualAsset as? AVURLAsset)?.url.absoluteString
            {
                PRINT("VIDEO URL: \(strURL)")
                callBack(URL.init(string: strURL))
            }
        })
    }
    

    【讨论】:

      【解决方案2】:

      从 QBImagePickerController 中的 didFinishPickingAssets 从 Swift 4.2 中的 PHAsset 获取视频 URL

      let imagePicker = QBImagePickerController()
      imagePicker.delegate = self
      imagePicker.numberOfColumnsInPortrait = 2
      imagePicker.maximumNumberOfSelection = 2
      imagePicker.allowsMultipleSelection = true
      imagePicker.mediaType = .video
              
      self.present(imagePicker, animated: true, completion: nil)
      
      func qb_imagePickerController(_ imagePickerController: 
      QBImagePickerController!, didFinishPickingAssets assets: [Any]!) {
          for asset in assets
          {
              PHCachingImageManager().requestAVAsset(forVideo: asset as! PHAsset, 
              options: nil, resultHandler: {(asset, audioMix, info) -> Void in
                  if asset != nil {
                      let avasset = asset as! AVURLAsset
                      let urlVideo = avasset.url
                      yourarray.add(urlVideo as URL)
                  }
                  
              })
      
          }
      
          
          imagePickerController.dismiss(animated: true) {
               //You can access video urls here
               print("Total Videos:\(yourarray.count)")
          }
          
       }
      

      【讨论】:

        【解决方案3】:

        对于 Swift 3.0 您可以从 PHAsset 对象中获取图像和视频的字符串 url

        asset.requestContentEditingInput(with: PHContentEditingInputRequestOptions(), completionHandler: { (contentEditingInput, dictInfo) in
        
            if asset.mediaType == .image
            {
              if let strURL = contentEditingInput?.fullSizeImageURL?.absoluteString
              {
                 print("IMAGE URL: ", strURL)
              }
            }
            else
            {
              if let strURL = (contentEditingInput!.avAsset as? AVURLAsset)?.url.absoluteString
              {
                 print("VIDEO URL: ", strURL)
              }
            }
          })
        

        【讨论】:

          【解决方案4】:

          说到PHAsset的视频url,我曾经在Swift 2上准备了一个实用函数(播放PHAsset的视频)。分享这个答案,可能会对某人有所帮助。

          static func playVideo (view:UIViewController, asset:PHAsset)
          

          请查看Answer

          【讨论】:

            【解决方案5】:

            顺便说一下,如果您想要实际的 URL——也就是说,与 UIImagePickerController 在创建 AVPlayerItem 时传递给它的 URL 相同,并且与 AVAssetReader/Writer 一起使用的 URL 看起来相同--然后这样做:

            [[PHImageManager defaultManager] requestAVAssetForVideo:phAsset options:nil resultHandler:^(AVAsset *avAsset, AVAudioMix *audioMix, NSDictionary *info) {
                NSURL *url = (NSURL *)[[(AVURLAsset *)avAsset URL] fileReferenceURL];
                NSLog(@"url = %@", [url absoluteString]);
                NSLog(@"url = %@", [url relativePath]);
             }];
            

            而 phAsset 是 PHAsset 对象,而 avAsset 是由 PHImageManager 生成的结果 AVAsset 对象,上述代码向控制台的输出将产生,例如:

            2016-04-16 01:15:40.155 ChromaEpsilon[3423:933358] url = file:///.file/id=16777218.8262005
            2016-04-16 01:15:40.155 ChromaEpsilon[3423:933358] url = /private/var/mobile/Media/DCIM/108APPLE/IMG_8421.MOV
            

            【讨论】:

            • 你能不能给这个 swift 3 版本。
            【解决方案6】:

            你应该使用PHImageManager

            使用类的 sharedInstance 并使用您的 PHAsset、选项和完成块处理程序调用此方法:

            - (PHImageRequestID)requestAVAssetForVideo:(PHAsset *)asset options:(PHVideoRequestOptions *)options resultHandler:(void (^)(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info))resultHandler
            

            处理程序会给你一个AVAsset,你应该使用你的AVPlayerItem,而不是一个URL。

            例子:

            [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:nil resultHandler:^(AVAsset *avAsset, AVAudioMix *audioMix, NSDictionary *info) {
              // Use the AVAsset avAsset
              AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:avAsset];
              AVPlayer *videoPlayer = [AVPlayer playerWithPlayerItem:playerItem];
            }];
            

            注意它可能是异步的,这会进一步干扰您的应用程序流程。

            【讨论】:

            • 我试过这个,但我不知道把这段代码放在哪里。我在完成资产挑选后尝试放置它,但随后出现错误。我真的几乎没有 Objective-c 经验
            • 请将更新后的代码添加到您的问题中,以便我查看@TomFox
            • 当我说“调用”该方法时,我的意思是你应该使用 [[PHImageManager sharedInstance] requestAVAssetForVideo...] @TomFox ,抱歉有任何混淆。我没有完整的代码 sn-p 方便使用,所以我只是将文档中的方法声明放在我的答案中
            • 我添加了一个例子。 See this also.
            猜你喜欢
            • 2016-11-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-02-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多