为 PHAsset 获取 url

Getting url for PHAsset

我正在使用自定义图像选择器来选择时间流逝,我想获得所选时间流逝的 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 编写的。所以我有点objective-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];
}

你应该使用 PHImageManager.

使用 class 的 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];
}];

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

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

[[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

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

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

请检查这个

For Swift 3.0 You can get string url for image and video from PHAsset object

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)
      }
    }
  })

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

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))
        }
    })
}

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

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)")
    }
    
 }