ALAssetsLibrary为Nil,无法将视频保存到相册

时间:2015-05-20 05:07:57

标签: ios iphone swift alassetslibrary

我无法使用ALAssetsLibrary将来自网址的视频写入我保存的相册。我不确定如何正确初始化库。 Apple对ALAssetsLibrary的引用并没有真正解释如何这样做。我得到通常的展开nil值错误。

有人能指出我在正确的方向吗?

我很乐意接受任何提示,以及我如何提高效率,或者指导和教程可以教会我如何。

这是我的代码,告诉相机拍照或开始录制。截至目前,我只关注录音,因为它将是我应用程序的主要功能。

/**************************************************************************
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")
    }
}

这是我在视频开始和完成录制时调用的委托函数。

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
    }

我把这个库作为该类的可选属性。

0 个答案:

没有答案