美文网首页
Day 4 - ScrollView,视频采集

Day 4 - ScrollView,视频采集

作者: Codepgq | 来源:发表于2016-09-08 15:36 被阅读41次

    不完整效果图如下

    不完整效果图

    今天了解学习ScrollView

    1、搭建UI

    这里我们使用XIB
    拖入ScrollView,添加约束,上下左右 0


    拖入

    <br />
    约束


    约束
    • gif中可以看到有三个View,这里我们创建三个VC,并且使用XIB
      1、command + n
      2、选择IOS下的Source
      3、选择Cocoa Touch Class
      4、语言选择Swift
      5、勾上XIB


      如图

    重复1-5步创建三个VC

    三个VC
    • 分别在三个VC的XIB中添加一个ImageView,在CenterViewController中再添加一个View,用于显示视频采集


      如图
    在分别拖线到各自的VC中。

    来到ViewController中,加载三个VC,并且把VC的View添加到ScrollView中

    let leftVC :LeftViewController = LeftViewController(nibName: "LeftViewController", bundle: nil)
            let centerVC :CenterViewController = CenterViewController(nibName: "CenterViewController", bundle: nil)
            let rightVC :RightViewController = RightViewController(nibName: "RightViewController", bundle: nil)
            
            
            self.addChildViewController(leftVC)
            self.scrollView.addSubview(leftVC.view)
            leftVC.didMoveToParentViewController(self)
            
            
            self.addChildViewController(centerVC)
            self.scrollView.addSubview(centerVC.view)
            centerVC.didMoveToParentViewController(self)
            
            self.addChildViewController(rightVC)
            self.scrollView.addSubview(rightVC.view)
            rightVC.didMoveToParentViewController(self)
            
            var centerFrame : CGRect = centerVC.view.frame
            centerFrame.origin.x = self.view.frame.width
            centerVC.view.frame = centerFrame
            
            var rightFrame : CGRect = rightVC.view.frame
            rightFrame.origin.x = self.view.frame.width * 2
            rightVC.view.frame = rightFrame
            
            self.scrollView.contentSize = CGSize(width: self.view.frame.width * 3, height: 0)
            self.scrollView.pagingEnabled = true
    

    <br />

    2、为第一个ImageView添加手势、动画

    进入LeftViewController,如果你的命名不同就自行对应。
    添加手势

    let pan: UIPinchGestureRecognizer? = UIPinchGestureRecognizer(target: self, action: #selector(LeftViewController.pinchEvent(_:)))
            imageView.userInteractionEnabled = true
            imageView.addGestureRecognizer(pan!)
    

    <br />
    实现手势方法

    func pinchEvent(pinch : UIPinchGestureRecognizer){
    //手势
            let scale : CGFloat = pinch.scale
            pinch.view?.transform = CGAffineTransformMakeScale(scale, scale)
            
    //动画
            if pinch.state == UIGestureRecognizerState.Ended {
                UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.35, initialSpringVelocity: 0, options: UIViewAnimationOptions.CurveLinear, animations: {
                        pinch.view?.transform = CGAffineTransformIdentity
                    }, completion: { (a) in
                        print("animation finish")
                })
            }
        }
    

    <br />

    3、视频采集

    这里有概念介绍:大神地址

    import UIKit
    import AVFoundation
    
    class CenterViewController: UIViewController , UIImagePickerControllerDelegate,UINavigationControllerDelegate{
    
        @IBOutlet weak var cameraView: UIView!
        @IBOutlet weak var imageView: UIImageView!
        
        var captureSession : AVCaptureSession?
        var stillImageOutPut : AVCaptureStillImageOutput?
        var preViewLayer : AVCaptureVideoPreviewLayer?
        
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            UIApplication.sharedApplication().statusBarHidden = true
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        
        
        override func viewDidAppear(animated: Bool) {
            super.viewWillAppear(animated)
            preViewLayer?.frame = self.view.bounds
        }
        
        override func viewWillAppear(animated: Bool) {
            
            super.viewWillAppear(animated)
            
            //创建对象
            captureSession = AVCaptureSession()
            //分辨率
            captureSession?.sessionPreset = AVCaptureSessionPreset1920x1080
            
            let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
            var error : NSError?
            var input : AVCaptureDeviceInput!
            
            do{
                input = try AVCaptureDeviceInput(device: backCamera)}
            catch let error1 as NSError{
                error = error1
                input = nil
            }
            
            if error == nil && captureSession?.canAddInput(input) != nil
            {
                captureSession?.addInput(input)
                
                stillImageOutPut = AVCaptureStillImageOutput()
                stillImageOutPut?.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]
                
                if captureSession?.canAddOutput(stillImageOutPut) != nil
                {
                    captureSession?.addOutput(stillImageOutPut)
                    
                    preViewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                    preViewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
                    preViewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.Portrait
                    cameraView.layer.addSublayer(preViewLayer!)
                    captureSession?.startRunning()
                }
            }
        }
        
        override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
            super.touchesBegan(touches, withEvent: event)
            
            //点击屏幕保存图片
            showImage()
            
        }
        
        
        var didTakePhoto = Bool()
        
        func showImage(){
            if didTakePhoto == true {
                self.imageView.hidden = true
                didTakePhoto = false
            }else{
                didTakePhoto = true
                captureSession?.startRunning()
                clickScreenTakePhoto()
            }
        }
        
        
        
        func clickScreenTakePhoto(){
            if let videoConnection = stillImageOutPut?.connectionWithMediaType(AVMediaTypeVideo) {
                //方向
                videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
                stillImageOutPut?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { (sampleBuffer, error) in
                    
                    if sampleBuffer != nil{
                        let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                        let dataProvider = CGDataProviderCreateWithCFData(imageData)
                        let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, CGColorRenderingIntent.RenderingIntentDefault)
                        let image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)
                        
                        self.imageView.image = image
                        self.imageView.hidden = false
                        
                        UIImageWriteToSavedPhotosAlbum(image, self, #selector(CenterViewController.image(_:didFinishSavingWithError:contextInfo:)),nil)
                    }
                })
            }
        }
        
        func image(image: UIImage, didFinishSavingWithError: NSError?, contextInfo: AnyObject) {
            
            if didFinishSavingWithError == nil {
                showSuccessView()
                return
            }
            print("OK")
        }
        
        func showSuccessView(){
            let alert : UIAlertController? = UIAlertController(title: "提示", message: "图片保存成功咯", preferredStyle: UIAlertControllerStyle.Alert)
            
            let action: UIAlertAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Cancel) { (cancel) in
                
            }
            alert?.addAction(action)
            self.presentViewController(alert!, animated: true, completion: nil)
        }
        
    }
    
    

    OK,到这里就完成今天的东西了

    DEMO地址

    相关文章

      网友评论

          本文标题:Day 4 - ScrollView,视频采集

          本文链接:https://www.haomeiwen.com/subject/kpmsettx.html