美文网首页
Swift 使用GPUImage写一个简单的滤镜相机DEMO

Swift 使用GPUImage写一个简单的滤镜相机DEMO

作者: 咸鱼有只喵 | 来源:发表于2018-07-25 17:28 被阅读116次

    1.集成

    集成GPUImage 的方法和其他第三方是一样的,用cocopod就好啦:

    pod install
    

    然后就是具体的实现:

    过程还是蛮简单的,这里使用photosKit来保存图片,因为还在学习中,具体的解析就不写辣,学会了再补充:

    import UIKit
    import Photos
    import GPUImage
    
    
    class FConViewController: UIViewController {
        
        var mCamera:GPUImageStillCamera!
        var mFillter:GPUImageFilter!
        var mGpuimageView:GPUImageView!
        
    
        override func viewDidLoad() {
            super.viewDidLoad()
            mCamera = GPUImageStillCamera(sessionPreset:AVCaptureSession.Preset.vga640x480.rawValue , cameraPosition: AVCaptureDevice.Position.back)
            
            mCamera.outputImageOrientation = UIInterfaceOrientation.portrait
            
            //滤镜
            mFillter = GPUImageSingleComponentGaussianBlurFilter()
            
            mGpuimageView = GPUImageView(frame:view.bounds)
            
            mCamera.addTarget(mFillter)
            mFillter.addTarget(mGpuimageView)
            view.addSubview(mGpuimageView)
            mCamera.startCapture()
            
            var btn = UIButton(frame: CGRect(x: (view.bounds.size.width - 80) * 0.5, y: view.bounds.size.height - 60, width: 80, height: 40))
            btn.backgroundColor = UIColor.red
            btn.setTitle("拍照", for: .normal)
            view.addSubview(btn)
            btn.addTarget(self, action: #selector(self.takePhoto), for: .touchUpInside)
        
            // Do any additional setup after loading the view.
        }
    
        
        @objc func  takePhoto(){
            mCamera.capturePhotoAsJPEGProcessedUp(toFilter: mFillter, withCompletionHandler: {
                processedJPEG, error in
                
                
                
                if let aJPEG = processedJPEG {
                    let imageview = UIImage(data: aJPEG)
                    UIImageWriteToSavedPhotosAlbum(imageview!, nil, nil, nil)
                    
                }
                
     
                
                
            })
            
            
            
            
        }
        
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        
    
        /*
        // MARK: - Navigation
    
        // In a storyboard-based application, you will often want to do a little preparation before navigation
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            // Get the new view controller using segue.destinationViewController.
            // Pass the selected object to the new view controller.
        }
        */
    
    }
    
    

    奥对了,记得在info.plist添加相册权限哦!

    相关文章

      网友评论

          本文标题:Swift 使用GPUImage写一个简单的滤镜相机DEMO

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