swift - 简单的图片滤镜+保存view转成图片存入本地相册

前端之家收集整理的这篇文章主要介绍了swift - 简单的图片滤镜+保存view转成图片存入本地相册前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import UIKit
import ZLLTools
import PhotoFramework
import Photos
import AssetsLibrary
class ViewController: UIViewController {
    var imageView :UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        GetPhoto.get()
         imageView = UIImageView.init(frame: CGRect.init(x: 50,y: 50,width: 300,height: 200))
        self.view.addSubview(imageView)
        imageView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer.init(target: self,action: #selector(self.testClick))
        imageView.addGestureRecognizer(tap)
        
        imageView.image = ZLLToolCategory.getVideoFirstViewImage(videoUrlString: "http://gslb.miaopai.com/stream/1UKfVpOmazRYEb4fVejwhgpX~3uIxmHBV~8VCQ__.mp4")//获取视频第一帧
    
        
        let btn = UIButton.init(frame: CGRect.init(x:(375) / 2,y: 200 + 20 + 50,width: 60,height: 40))
        btn.setTitle("滤镜",for: .normal)
        btn.backgroundColor = UIColor.red
        btn.addTarget(self,action: #selector(滤镜),for: .touchUpInside)
        self.view.addSubview(btn)
        
        let picBtn = UIButton.init(frame: CGRect.init(x:(375) / 2,y: 200 + 20 + 60 + 50,width: 100,height: 40))
        picBtn.setTitle("保存图片",for: .normal)
        picBtn.backgroundColor = UIColor.blue
        picBtn.addTarget(self,action: #selector(saveBtnClick),for: .touchUpInside)
        self.view.addSubview(picBtn)
    }
    @objc func testClick() {
        //选择图片
        _ = presentImagePicker(maxSelected: 4,completeHandler: {[weak self] (assets) in
            for asset in assets {
                print(asset)
                //设置图片尺寸
                let size = CGSize.init(width: asset.pixelWidth,height: asset.pixelHeight)
                //获取图片
                PHImageManager.default().requestImage(for: asset,targetSize: size,contentMode: .default,options: nil,resultHandler: { (image,info) in
                    self?.imageView.image = image
                })
            }
        })
    }
    
    @objc func 滤镜(){
        let inputImage = CIImage.init(image: imageView.image!)
//        let filter = CIFilter.init(name: "CIAffineTransform")//倾斜
//        let filter = CIFilter.init(name: "CISepiaTone")//棕褐色
//        let filter = CIFilter.init(name: "CIPhotoEffectNoir")//黑白
        let filter = CIFilter.init(name: "CIPhotoEffectNoir")
        filter?.setValue(inputImage,forKey: kCIInputImageKey)
//        filter?.setValue(NSValue.init(cgAffineTransform: CGAffineTransform.init(a: 0.7,b: 0.5,c: 0.3,d: 1.0,tx: 0.0,ty: 0.0)),forKey: "inputTransform")//倾斜
//        filter?.setValue(0.5,forKey: "inputIntensity")//棕褐色
    
//        filter?.setValue(0.5,forKey: "inputIntensity")

        let outputImage = filter?.outputImage
        let cgImage = CIContext.init(options: nil).createCGImage(outputImage!,from: (outputImage?.extent)!)
        imageView.image = UIImage.init(cgImage: cgImage!)
        
    }
    
    //将view转成图片并保存相册
    @objc func saveBtnClick(){
        let frame = imageView.frame
        UIGraphicsBeginImageContext(frame.size)
        imageView.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        self.loadImage(image: image!)
    }
    
    //保存图片
    func loadImage(image:UIImage){
        
        UIImageWriteToSavedPhotosAlbum(image,self,#selector(self.image(image:didFinishSavingWithError:contextInfo:)),nil)
    }

    @objc func image(image: UIImage,didFinishSavingWithError: NSError?,contextInfo: AnyObject)
    {
        if didFinishSavingWithError != nil
        {
            print("error!")
            return
        }
        
        print("保存成功")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
原文链接:https://www.f2er.com/swift/320903.html

猜你在找的Swift相关文章