【问题标题】:How I can access video/Image from AVURLAsset URL in swift 3.3x?如何在 swift 3.3x 中从 AVURLAsset URL 访问视频/图像?
【发布时间】:2017-11-03 15:59:24
【问题描述】:

我有类似“file:///var/mobile/Media/DCIM/101APPLE/IMG_1006.MOV”的 AVUrlAsset。如何在 Swift 中从这个 url 访问视频?

【问题讨论】:

    标签: ios swift3 avurlasset


    【解决方案1】:

    这样你就可以获取所有视频

            DispatchQueue.global(qos: .background).async {
    
            PHPhotoLibrary.requestAuthorization { (status) -> Void in
    
                let allVidOptions = PHFetchOptions()
                allVidOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.video.hashValue) //Any type you want to fetch
                allVidOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
    
    
    //Now the set the option to `fetchAssets`
                let allVids = PHAsset.fetchAssets(with: allVidOptions)
    
                print("All Videos Count \(allVids.count)")
    
                for index in 0..<allVids.count {
    
                    let videoRequestOptions =           PHVideoRequestOptions()
                    videoRequestOptions.deliveryMode = .fastFormat //quality-> 360p mp4
                    videoRequestOptions.version =      .original
    
    
                    PHImageManager.default().requestPlayerItem(forVideo: allVids[index], options: videoRequestOptions , resultHandler: { (playerItem, result) in
    
                        // print(result as! [String: AnyObject])
                        // print(playerItem)
    
                        let currentVideoUrlAsset = playerItem?.asset as?  AVURLAsset
    
                        let currentVideoFilePAth = currentVideoUrlAsset!.url
    
                        let lastObject = currentVideoFilePAth.pathExtension
    
                        print(lastObject)
    
                        if lastObject == "M4V" {
    
                            self.arrOfVideos.append(playerItem!)
    
                        }
    
    
                        //NSString *lastPath = [videoURL lastPathComponent];
                        //NSString *fileExtension = [lastPath pathExtension];
                        //NSLog(@"File extension %@",fileExtension);
    
    
                        var i = Int()
                        print("Appending.... \(i);)")
                        i += 1
    
                        print("My Videos Count \(self.arrOfVideos.count)")
    
    
                        DispatchQueue.main.async(execute: {
    
                            self.tblVideos.reloadData()
                        })
    
                    })
    
                    //fetch Asset here
                    //print(allVids[index].description)
                    //   print(self.arrOfVideos) //will show empty first then after above completion the execution will come here again
    
                }
    
            }
    
        }
    

    【讨论】:

    • 你有AVUrlAsset ?
    • 可以说我只有 url (file:///var/mobile/Media/DCIM/101APPLE/IMG_1006.MOV) 就是这样,那么我该如何访问媒体呢? iOS 不允许直接访问该文件路径权限
    • 你可以通过PHImageManager访问,你会得到playerItem
    • 我已经更新了我的答案看看!我想这就是你要找的东西
    • @Ganesh 如果您发现任何困难,请告诉我!
    【解决方案2】:

    我想,如果你知道 url 路径。那么你可以这样做:

    let urlStr = URL.init(fileURLWithPath:"file:///var/mobile/Media/DCIM/101APPLE/IMG_1006.MOV") 
    let asset = AVURLAsset.init(url: urlStr)
    

    apple reference 有帮助

    【讨论】:

    • 但是当我尝试将 url 转换为 data 时,即让 data = NSData(contentsOf:asset.url) 在这里得到 nil
    • 我建议,让 data = Data.init(contentsOf:asset.url)
    猜你喜欢
    • 1970-01-01
    • 2018-08-13
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    相关资源
    最近更新 更多