2016-01-13 15 views
6

ALAssetsLibrary ist in diesen Tagen veraltet, aber praktisch alle Beispiele auf SO nutzen es noch. Für mein Ziel muss ich die URL des Videos wissen, das der Fotobibliothek hinzugefügt wurde, damit ich ein Video mit der Instagram-App teilen kann (nur so akzeptiert Instagram Videos). Die URL sollte wahrscheinlich beginnen mit "Vermögen-Bibliothek: // ..."URL eines neu erstellten Assets abrufen, iOS 9-Stil

Dieses einfach mit ALAssetsLibrary:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
[library writeVideoAtPathToSavedPhotosAlbum:videoFilePath completionBlock:^(NSURL *assetURL, NSError *error) { // ... 

oben, wird die URL als Argument für den Abschluss Block übergeben. Aber was ist mit iOS 9 kompatiblen Weg mit PHPhotoLibrary Klasse und performChanges Methode?

var videoAssetPlaceholder:PHObjectPlaceholder! // property 

// ... 

let videoURL = NSBundle.mainBundle().URLForResource("Video_.mp4", withExtension: nil)! 

let photoLibrary = PHPhotoLibrary.sharedPhotoLibrary() 
photoLibrary.performChanges({ 
    let request = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(videoURL) 
    self.videoAssetPlaceholder = request?.placeholderForCreatedAsset 
}, 
completionHandler: { success, error in 
    if success 
    { 
     // The URL of the video asset? 
    } 
}) 
+0

Warum Sie Sie nicht nur lokal Video speichern Sie es vor zu teilen? –

+0

@LeoDabus So wie Videos geteilt werden, kann Instagram kein Video aus der Sandbox meiner App lesen –

Antwort

16

Hier ist, was ich mit endete haben:

let videoURL = NSBundle.mainBundle().URLForResource("Video_.mp4", withExtension: nil)! 

let photoLibrary = PHPhotoLibrary.sharedPhotoLibrary() 
var videoAssetPlaceholder:PHObjectPlaceholder! 
photoLibrary.performChanges({ 
    let request = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(videoURL) 
    videoAssetPlaceholder = request!.placeholderForCreatedAsset 
}, 
completionHandler: { success, error in 
    if success { 
     let localID = videoAssetPlaceholder.localIdentifier 
     let assetID = 
      localID.stringByReplacingOccurrencesOfString(
       "/.*", withString: "", 
       options: NSStringCompareOptions.RegularExpressionSearch, range: nil) 
     let ext = "mp4" 
     let assetURLStr = 
      "assets-library://asset/asset.\(ext)?id=\(assetID)&ext=\(ext)" 

     // Do something with assetURLStr 
    } 
}) 
+0

Funktioniert wunderbar. – 209135

4

Nizza Antwort und Pseudo dank Desmond. Hier

ist eine aktualisierte Antwort:

Swift 3

var assetPlaceholder: PHObjectPlaceholder! 
    PHPhotoLibrary.shared().performChanges({ 
     let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: videoURL as URL!) 
     assetPlaceholder = assetRequest?.placeholderForCreatedAsset 
    }, completionHandler: { (success, error) in 
     if success { 
      let localID = assetPlaceholder.localIdentifier 
      let assetID = localID.replacingOccurrences(of: "/.*", with: "", options: NSString.CompareOptions.regularExpression, range: nil) 
      let ext = "mp4" 
      let assetURLStr = "assets-library://asset/asset.\(ext)?id=\(assetID)&ext=\(ext)" 

      // Do what you want with your assetURLStr 
     } else { 
      if let errorMessage = error?.localizedDescription { 
       print(errorMessage) 
      } 
     } 
    }) 

geht weiter

Obwohl diese perfekt für den Moment funktioniert, sollte jemand wissen, eine "saubere" Art und Weise zu Holen Sie sich diese "Assets-Library" URL, ohne diese "manuelle" Lösung?

fand ich dieses interesting thread, aber es gibt nur eine „file: /// var/...“ URL