繁体   English   中英

获取 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];
}

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

我通常使用 swift 但这个自定义图像选择器是用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];
}

顺便说一句,如果你想要实际的 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

您应该使用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];
}];

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

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

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

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

在从视频网址的speking PHAsset ,我曾经编写了斯威夫特2效用函数(播放视频PHAsset )。 分享这个答案,可能会对某人有所帮助。

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

请检查这个答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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