2017-06-02 190 views

回答

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 

      } 

     } 

    } 
+0

你''AVUrlAsset? –

+1

可以說我只是有網址(文件:///var/mobile/Media/DCIM/101APPLE/IMG_1006.MOV)多數民衆贊成它,所以我怎麼可以從中訪問媒體? iOS不允許直接訪問您通過'PHImageManager'訪問的文件路徑 – Ganesh

+0

,您將從中獲取'playerItem'。 –

2

我想,你已經知道的URL路徑。那麼你可以做如下:

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

apple reference幫助

+0

但是,當我嘗試將網址轉換爲數據,即 讓data = NSData(contentsOf:asset.url)在這裏變爲零 – Ganesh

+0

我建議讓數據= Data.init(contentsOf:asset.url) – dengApro

+0

謝謝您,但Still數據無法獲取。 – Ganesh