问题
I am having trouble using ALAssetsLibrary to write a video from a URL into my saved photos album. I am not sure how to go about initializing the library correctly. Apple's reference to ALAssetsLibrary doesn't really explain how to do so. I am getting the usual unwrapping nil value error.
Could anybody point me in the right direction here?
I'll gladly take any tips as well as to how I can make things more efficient, or guides and tutorials that could teach me how.
here is my code that tells the camera to take a picture or start recording. As of right now, I am only focused on recording, since it will be the main feature of my application.
/**************************************************************************
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")
}
}
and here are my delegate functions that are called when the video starts and finishes recording.
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
}
I am holding the library as an optional property of the class.
来源:https://stackoverflow.com/questions/30340689/alassetslibrary-is-nil-cant-save-video-to-photo-album