我使用了以下代码并没有成功保存电影 请告诉我我错在哪里:
我用过,
import UIKit
import MobileCoreServices
import Foundation
class RecordingViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate
代码本身是通过按钮激活的,
//Recording a video
@IBAction func recordANewVideoMessage(sender: UIButton) {
//Initiate camera
let picker = UIImagePickerController()
picker.delegate = self
if UIImagePickerController.isSourceTypeAvailable(.Camera)
{
picker.sourceType = .Camera
if let availableTypes = UIImagePickerController.availableMediaTypesForSourceType(.Camera) {
if (availableTypes as NSArray).containsObject(kUTTypeMovie) {
/* Set up the camera defaults, movie. no editing, 3 minutes max - consider presenting time during recording, medium quality */
picker.mediaTypes = [kUTTypeMovie]
picker.allowsEditing = false
picker.videoMaximumDuration = 180 // Perhaps reduce 180 to 120
picker.videoQuality = UIImagePickerControllerQualityType.TypeMedium
presentViewController(picker, animated: true, completion: nil) // presents the camera for the user
}
}
}
//In case the user cancels the movie
func imagePickerControllerDidCancel(UIImagePickerController) {
presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let uniqueVideoName = "/temporaryVideoName" //will be altered when we have something unique for the user
var video = info[UIImagePickerControllerReferenceURL] as! NSURL
let urlVideo = video.URLByAppendingPathComponent("\(uniqueVideoName)"+".mpeg4").absoluteString
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlVideo))
{
UISaveVideoAtPathToSavedPhotosAlbum(urlVideo, nil, nil, nil)
}
presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
}
}
答案 0 :(得分:0)
这是一种在swift中将视频保存到相机胶卷的方法:
import Photos
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
self.dismissViewControllerAnimated(true, completion: nil)
if let referenceURL = info[UIImagePickerControllerReferenceURL] as? NSURL {
let fetchResult = PHAsset.fetchAssetsWithALAssetURLs([referenceURL], options: nil)
if let phAsset = fetchResult.firstObject as? PHAsset {
PHImageManager.defaultManager().requestAVAssetForVideo(phAsset, options: PHVideoRequestOptions(), resultHandler: { (asset, audioMix, info) -> Void in
if let asset = asset as? AVURLAsset {
let videoData = NSData(contentsOfURL: asset.URL)
// write the video to the temp directory
let videoPath = NSTemporaryDirectory() + "tmpMovie.MOV"
let videoURL = NSURL(fileURLWithPath: videoPath)
videoData?.writeToURL(videoURL, atomically: true)
let assetLibrary = ALAssetsLibrary()
assetLibrary.writeVideoAtPathToSavedPhotosAlbum(videoURL, completionBlock: { (url, error) -> Void in
if error == nil {
print("saved")
}
else {
print(error)
}
})
}
})
}
}
}