博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
swift - 简单的图片滤镜+保存view转成图片存入本地相册
阅读量:6582 次
发布时间:2019-06-24

本文共 3823 字,大约阅读时间需要 12 分钟。

import UIKitimport ZLLToolsimport PhotoFrameworkimport Photosimport AssetsLibraryclass 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://my.oschina.net/wayzhu/blog/1580978

你可能感兴趣的文章
删除浏览器浏览器删除cookie方法
查看>>
微软URLRewriter.dll的url重写的简单使用(实现伪静态)
查看>>
leetcode -- Combination Sum II
查看>>
1z0-052 q209_7
查看>>
PIN码计算锦集
查看>>
[Unity3D]再次点击以退出程序
查看>>
架构师的97种习惯
查看>>
PHP 开发 APP 接口 学习笔记与总结 - XML 方式封装通信接口
查看>>
IT基础架构规划方案之实际网络设计案例
查看>>
Navicat for MySQL 使用SSH方式链接远程数据库(二)
查看>>
poj 1274The Perfect Stall
查看>>
ibm BIP2276E: The flow includes a message flow of node type 'ComIbmFileReadNode'
查看>>
HDU 4720 Naive and Silly Muggles (外切圆心)
查看>>
垂死挣扎还是涅槃重生 -- Delphi XE5 公布会归来感想
查看>>
Ubuntu上运行Blender,在控制台上查看运行结果
查看>>
怎么检查网站的死链接呢?
查看>>
scrapy爬虫框架实例一,爬取自己博客
查看>>
React是UI的未来吗?
查看>>
中国人社部:2018年15个省(区、市)调整最低工资标准
查看>>
手把手教你通过Thrift 访问ApsaraDB for HBase
查看>>