ALAssetsLibrary为零,无法将video保存到相册

我无法使用ALAssetsLibrary从URL中将video写入保存的照片相册。 我不知道如何正确地初始化库。 苹果对ALAssetsLibrary的引用并没有真正解释如何去做。 我得到了通常的解包零值错误。

任何人都可以在这里指出正确的方向吗?

我会很乐意采取任何提示,以及如何使事情更有效率,或指导和教程,可以教我如何。

这是我的代码,告诉相机拍照或开始录制。 到目前为止,我只专注于录音,因为这将是我的应用程序的主要特点。

/************************************************************************** DID PRESS CAPTURE **************************************************************************/ @IBAction func didPressCapture(sender: AnyObject) { if self.takingVideo == true { //------ MAKE SURE WE ARE NOT ALREADY RECORDING ------ if self.weAreRecording == false { println("Taking a video") //------ MAKE SURE THE DEVICE IS AUTHORIZED TO TAKE A VIDEO ------ if self.deviceAuthorized == AVAuthorizationStatus.Authorized { println("Device is authorized to take video") println("Getting Video Output Path") //------ GRAB THE OUTPUT FILE URL TO SAVE TO PHOTO ALBUM ------ let outputPath = "\(self.documentsPath)/output.mov" self.outputFileURL = NSURL(fileURLWithPath: outputPath) self.session.startRunning() println("Starting to record to output path") if let movieOutput = self.movieFileOutput { movieOutput.startRecordingToOutputFileURL(self.outputFileURL!, recordingDelegate: self) } else { println("Movie file output is NIL!!!!") } } else { println("Device is not authorized to take video") } } else { //------ STOP THE VIDEO, PROCESS THE VIDEO HERE ------ println("Stopping the video") println("Stopping the session from recording") //------ STOP RECORDING AND SAVE TO VIDEO TO PHOTO ALBUM ------ self.session.stopRunning() self.movieFileOutput!.stopRecording() } } else { //------ HANDLE TAKING A PICTURE HERE ------ println("Taking a picture") } } 

这里是我的委托函数,当video开始并结束录制时被调用。

 func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) { println("capture output : finish recording to \(outputFileURL)") self.weAreRecording = false self.isSessionRunning = false println("Initializing assets library") self.library = ALAssetsLibrary() if let library = self.library { if library.videoAtPathIsCompatibleWithSavedPhotosAlbum(outputFileURL) { // error happens here, even though I am checking to see if it is nil before we get here self.library!.writeVideoAtPathToSavedPhotosAlbum(outputFileURL, completionBlock: { (outputURL, error) -> Void in if error == true { println("There was an error saving the video, which is: \(error.description)") } else { NSFileManager.defaultManager().removeItemAtURL(outputURL, error: nil) } }) println("Got pass saving the video") } else { println("Video is not compatible with saved photos album") } } else { println("Assets Library is nil!") } } func captureOutput(captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAtURL fileURL: NSURL!, fromConnections connections: [AnyObject]!) { println("capture output: started recording to \(fileURL)") self.isSessionRunning = true self.weAreRecording = true } 

我把图书馆作为这个class级的一个可选的财产。