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)
}
}