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

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

概述import UIKitimport ZLLToolsimport PhotoFrameworkimport Photosimport AssetsLibraryclass ViewController: UIViewController { var imageView :UIImageView! override func viewDidLoad() {
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.    }}
总结

以上是内存溢出为你收集整理的swift - 简单的图片滤镜+保存view转成图片存入本地相册全部内容,希望文章能够帮你解决swift - 简单的图片滤镜+保存view转成图片存入本地相册所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1056720.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存