2013-01-22 76 views
4

是否可以將位於iOS照片庫的視頻文件複製到我們應用程序的文檔目錄中?我使用uiimagepickercontroller從我們獲取視頻文件的NSUrl的地方嘗試,然後將其轉換爲NSData,然後將其寫入文件。但不幸的是,它不工作。我有沒有其他方法?我們如何才能將視頻文件從庫複製到文檔目錄?

我的意圖是將視頻加載到OpenCV CvCapture。

+1

你看過這個:http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/CameraAndPhotoLib_TopicsForIOS/Articles/PickinganItemfromthePhotoLibrary.html#//apple_ref/doc/uid/TP40010408-​​SW1? 爲什麼不嘗試使用NSFileManager直接複製文件? –

+0

也許給我們展示一些代碼.. –

回答

0

嗨Isarathg這是IOS設備的經典用例。哪些不允許您直接使用路徑值訪問任何相冊資產。交叉檢查我的回答,請只是檢查FileExistsAtPath您的文件中像下面 - :

println(NSFileManager.defaultManager().fileExistsAtPath(urlvalue.path!)) 

O/P you will get => False 

我也有這個問題幾天內最終回到閱讀整個文檔IOS之後。我已經知道了「只有當我們打開PHImageManager會話時,我們才能訪問PhotoAlbum資產」。要檢查這種說法,請嘗試下面的代碼 - :

var currentVideofetch: PHFetchResult! 
required init(coder aDecoder: NSCoder) { 
    let options = PHFetchOptions() 
    options.sortDescriptors = [ 
     NSSortDescriptor(key: "creationDate", ascending: true) 
    ] 
    currentVideofetch = PHAsset.fetchAssetsWithMediaType(.Video, options: options) 
    super.init(coder: aDecoder) 
} 
func checkImageExists(){ 
let asset = self.currentVideofetch.objectAtIndex(1) as? PHAsset 
} 
if let checkedAsset = asset { 
     PHImageManager.defaultManager().requestAVAssetForVideo(checkedAsset, options: nil, resultHandler: {[weak self](result: AVAsset!, audioMix: AVAudioMix!, info: [NSObject : AnyObject]!) in 
      println(NSFileManager.defaultManager().fileExistsAtPath(self.urlvalue.path!)) 
      }) 
    } 
O/P you will get => True 

打開PHImageManager會話,然後當我試圖訪問視頻與路徑。它運行良好。還可以使用相冊中視頻的相對路徑將所有視頻文件成功複製到我們的本地應用程序目錄。

如果你需要我可以給你我的這個實施。但不知道這是否是正確的方式。但爲我工作得很好。

其次,也是最有效的解決方案,我發現是使用

AVAssetExportSession

它的工作原理就像一個魅力。我的實施如下 - :

func importVideoToAppDir(videoURL: NSURL, videoFinalPath: NSURL, handler: ((NSURL?) -> Void)?) { 

    var assetDuration: CMTime! 
    var asset: AVAsset! 

    asset = AVAsset.assetWithURL(videoURL) as! AVAsset 
    assetDuration = asset!.duration 
    if (DirOperations.DeleteIfExists(videoTempPath) && DirOperations.DeleteIfExists(videoFinalPath)) { 
     let startTime = kCMTimeZero 
     let assetDurationSeconds = CMTimeGetSeconds(self.asset!.duration) 
     var range: CMTimeRange! 
     if assetDurationSeconds > Float64(maxDuration) { 
      let stopTime = CMTimeMakeWithSeconds(Float64(maxDuration), 1) 
      range = CMTimeRangeFromTimeToTime(startTime, stopTime) 
      } else { 
       let stopTime = CMTimeMakeWithSeconds(assetDurationSeconds, 1) 
       range = CMTimeRangeFromTimeToTime(startTime, stopTime) 
      } 
      var exporter :AVAssetExportSession = AVAssetExportSession(asset: self.asset, presetName: AVAssetExportPresetHighestQuality) 
      exporter.outputURL = videoFinalPath 
      exporter.outputFileType = AVFileTypeQuickTimeMovie 
      exporter.timeRange = range 
      exporter.exportAsynchronouslyWithCompletionHandler {() -> Void in 
       switch exporter.status { 
        case AVAssetExportSessionStatus.Failed: 
        println("failed import video: \(exporter.error)") 
        handler?(nil) 
        case AVAssetExportSessionStatus.Cancelled: 
        println("cancelled import video: \(exporter.error)") 
        handler?(nil) 
        default: 
        println("completed import video") 
        println(videoFinalPath) 
        handler?(videoFinalPath) 

       } 
      } 
    } 
} 

    func DeleteIfExists(path: NSURL) -> Bool { 
     var deleted = true 
     var error: NSError? 
     if (NSFileManager.defaultManager().fileExistsAtPath(path.path!)) { 
      deleted = NSFileManager.defaultManager().removeItemAtPath(path.path!, error: &error) 
     } 
     return deleted 
    } 

希望它有幫助。

+0

蘋果是否允許這種類型的實現,同時在AppStore中提交應用程序? –

相關問題