Project13-UISlider, CoreImage, UIImageWriteToSavedPhotosAlbum()

1.UISlider
UISlider的使用很简单,我们可以设置他的最大最小值,然后利用他的value属性拿到他的值。

2.利用CoreImage对image进行处理。
首先导入CoreImage,然后声明context和Filter

import CoreImage

///Core Image context, which is the Core Image component that handles rendering
var context: CIContext!
///Core Image filter, and will store whatever filter we have activated
var currentFilter: CIFilter!

再创建一个想要的filter

currentFilter = CIFilter(name: "CISepiaTone")

然后将image转换为CIImage,并且将这个ciimage设置给filter

//将UIImage转换为CIImage
let beginImage = CIImage(image: currentImage) 
currentFilter.setValue(beginImage, forKey: kCIInputImageKey) //给Filter设置一个image

然后为这个Filter设置他需要的属性的值

currentFilter.setValue(intensity.value, forKey: kCIInputIntensityKey)

最后将Filter的outputImage转换为CGImage,再转换成UIImage即可

 if let cgimg = context.createCGImage(currentFilter.outputImage!, from: currentFilter.outputImage!.extent) {
    let processedImage = UIImage(cgImage: cgimg)
    imageView.image = processedImage
}

3.将Image存储到PhotoAlbums中利用UIImageWriteToSavedPhotosAlbum( )方法

// Adds a photo to the saved photos album.  The optional completionSelector should have the form:
//  - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
public func UIImageWriteToSavedPhotosAlbum(_ image: UIImage, _ completionTarget: Any?, _ completionSelector: Selector?, _ contextInfo: UnsafeMutableRawPointer?)

我们可以看到,调用这个方法的时候需要给他传入一个selector.

例子如下:

//保存到相册
    @IBAction func save(_ sender: UIButton) {
        //the image to write
        //who to tell when writing has finished ,self
        //what method to call 
        //any context, just like th context value you can use with KVO
        guard let image = imageView.image else { return }
        UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contentxInfo:)), nil)
    }
    
    //- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
    func image(_ image: UIImage, didFinishSavingWithError error: Error?, contentxInfo: UnsafeRawPointer) {
        if let error = error {
            //翻译成用户可以看懂的错误提醒
            let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "OK", style: .default))
            present(ac, animated: true)
        } else {
            let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "OK", style: .default))
            present(ac, animated: true)
        }
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 最近在研究 Core Image 自定义 Filter 相关内容,重新学习了 Core Image,对 Co...
    泥孩儿0107阅读 790评论 0 4
  • --绘图与滤镜全面解析 概述 在iOS中可以很容易的开发出绚丽的界面效果,一方面得益于成功系统的设计,另一方面得益...
    韩七夏阅读 2,773评论 2 10
  • Core Image是一个强大的框架,它能够让你轻松地对图像进行过滤。你能够通过修改图像的饱和度、色调或曝光率来获...
    木易林1阅读 1,156评论 0 1
  • 许多UIView的子类,如一个UIButton或一个UILabel,它们知道怎么绘制自己。迟早,你也将想要做一些自...
    shenzhenboy阅读 1,686评论 2 8
  • 姑娘,请问你有没有男朋友。没有的话我希望我们可以交个朋友
    5adc213b1d02阅读 160评论 0 0