OStack程序员社区-中国程序员成长平台

标题: ios - 如何在 swift 3.3x 中从 AVURLAsset URL 访问视频/图像? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:27
标题: ios - 如何在 swift 3.3x 中从 AVURLAsset URL 访问视频/图像?

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



Best Answer-推荐答案


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

        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

            }

        }

    }

关于ios - 如何在 swift 3.3x 中从 AVURLAsset URL 访问视频/图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44325560/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4