获取PHAsset的URL

Tom*_*Fox 4 objective-c ios avplayer avkit

我使用自定义图像选择器来选择时间流逝,我想获取所选时间流逝的网址,以便我可以播放时间流逝.

- (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];
}
Run Code Online (Sandbox Code Playgroud)

这就是我现在所拥有的,但我需要获得资产的网址.

我通常使用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];
}
Run Code Online (Sandbox Code Playgroud)

Jam*_*ush 8

顺便说一句,如果你想要实际的URL - 也就是说,UIImagePickerController在创建AVPlayerItem时传递给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]);
 }];
Run Code Online (Sandbox Code Playgroud)

虽然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
Run Code Online (Sandbox Code Playgroud)


San*_*aus 6

你应该用PHImageManager.

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

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

处理程序将为您提供一个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];
}];
Run Code Online (Sandbox Code Playgroud)

请注意它可能的异步会进一步干扰您的应用程序流.


Hir*_*hal 5

对于 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)
      }
    }
  })
Run Code Online (Sandbox Code Playgroud)


iOS*_*fee 5

@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))
        }
    })
}
Run Code Online (Sandbox Code Playgroud)