2016-01-13 13 views
6

ALAssetsLibrary jest obecnie uznany za przestarzały, ale praktycznie wszystkie przykłady dotyczące SO nadal go wykorzystują. Aby osiągnąć ten cel, muszę znać adres URL filmu, który został dodany do biblioteki zdjęć, aby móc udostępnić film do aplikacji Instagram (to jedyny sposób, w jaki Instagram akceptuje filmy wideo). Adres URL powinien chyba zacząć "aktywa-biblioteki: // ..."Uzyskaj adres URL nowo utworzonego zasobu, styl iOS 9

To proste z ALAssetsLibrary:

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

Przede URL jest przekazywana jako argument do bloku zakończenia. Ale co z metodą zgodną z systemem iOS 9 z metodą PHPhotoLibrary i metodą performChanges?

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

Dlaczego nie można po prostu zaoszczędzić wideo lokalnie przed udostępnieniem go? –

+0

@LeoDabus Sposób, w jaki filmy są udostępniane, Instagram nie będzie w stanie odczytać wideo z piaskownicy mojej aplikacji. –

Odpowiedz

16

Oto co mam skończył z:

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

Działa pięknie. – 209135

4

Nicea odpowiedź i pseudo, dzięki Desmond.

Oto uaktualniony odpowiedź:

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) 
      } 
     } 
    }) 

Idąc dalej

Chociaż ta działa doskonale na chwilę, by ktokolwiek wie "czystszy" sposób zdobyć ten adres URL "asset-library", bez tego "ręcznego" rozwiązania?

Znalazłem to interesting thread, ale to tylko daje „file: /// var/...” URL

Powiązane problemy