2017-06-02 3 views

Antwort

1

diese Weise können Sie alle Videos

 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

Sie haben '' AVUrlAsset? –

+1

lässt sagen, ich habe nur URL (file: ///var/mobile/Media/DCIM/101APPLE/IMG_1006.MOV) das ist es, so wie ich von diesem Medium zugreifen kann? iOS erlaubt keinen direkten Zugriff auf den Dateipfad richtig – Ganesh

+0

Sie können über 'PHImageManager' zugreifen und Sie erhalten' playerItem' daraus. –

2

Ich denke holen können, haben Sie den URL-Pfad kannte. dann könnte man wie folgt vorgehen:

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

Die apple reference hilft

+0

Aber wenn ich versuche, URL in Daten zu konvertieren, dh lassen data = NSData (contentsOf: asset.url) ist hier immer nil – Ganesh

+0

Ich schlage vor, Daten lassen = Data.init (contentsOf: asset.url) – dengApro

+0

danke, aber immer noch werde in Daten Null. – Ganesh

Verwandte Themen