如何从swift 3.3x中的AVURLAsset URL访问视频/图像?

时间:2017-06-02 09:35:15

标签: ios swift3 avurlasset

我有AVUrlAsset这样的东西" file:///var/mobile/Media/DCIM/101APPLE/IMG_1006.MOV"。我如何在Swift中访问此网址中的视频?

2 个答案:

答案 0 :(得分:2)

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

        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

            }

        }

    }

答案 1 :(得分:2)

我想,你已经知道了url路径。然后你可以这样做:

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

apple reference帮助