效果图:
Untitled.gif这次直接上效果图,不然大家也不知道具体乱糟糟的代码是什么鬼。
首先项目需要这两个: AVFoundationimport 和 MobileCoreServices
1、先实例化一个按钮,这个就不用贴代码了吧。
2、然后初始化摄像机以及设置参数:
func initImagePicker(){
imagePicker = UIImagePickerController()
//设置imagePicker的来源,这里设置为摄像头
imagePicker.sourceType = .Camera
//设置为后摄像头
imagePicker.cameraDevice = .Rear
//设置摄像头的类型(录像)
imagePicker.mediaTypes = [String(kUTTypeMovie)]
//设置摄像头捕获模式
imagePicker.cameraCaptureMode = .Video
//是否允许编辑
imagePicker.allowsEditing = true
//设置摄像头的质量(高清)
imagePicker.videoQuality = .TypeIFrame1280x720
//设置代理检测操作
imagePicker.delegate = self
}
3、完成代理和设置保存路径:
extension ViewController: UINavigationControllerDelegate, UIImagePickerControllerDelegate {
//完成回调
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let url = info[UIImagePickerControllerMediaURL] as! NSURL
let urlStr = url.path
if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlStr!) {
//保存视频到相簿,注意也可以使用ALAssetsLibrary来保存
UISaveVideoAtPathToSavedPhotosAlbum(urlStr!, self, "video:didFinishSavingWithError:contextInfo:", nil)
print("保存成功......")
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Phone) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
print("取消录制")
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Phone) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
}
func video(videoPath: String, didFinishSavingWithError error: NSError, contextInfo info: AnyObject) {
print("保存成功")
let url = NSURL(fileURLWithPath: videoPath)
player = AVPlayer(URL: url)
let playerLayer = AVPlayerLayer(layer: player)
playerLayer.frame = imageView.bounds
imageView.layer.addSublayer(playerLayer)
player.play()
}
最近在学习多媒体后续还会写一些例子,大家多多指点。
自学笔记、谨慎阅读。
网友评论