Project13-UISlider、CoreImage、UIImageWriteToSavedPhotosAlbum()



Project13 Uislider Coreimage



1.UISlider
UISliderの使用は簡単です。彼の最大値と最小値を設定してから、彼のvalue属性を使用して彼の値を取得できます。

2.CoreImageを使用して画像を処理します。
最初にCoreImageをインポートし、次にコンテキストとフィルターを宣言します



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!

必要な別のフィルターを作成します

currentFilter = CIFilter(name: 'CISepiaTone')

次に、画像をCIImageに変換し、このciimageをフィルターに設定します



/ / Convert UIImage to CIImage let beginImage = CIImage(image: currentImage) currentFilter.setValue(beginImage, forKey: kCIInputImageKey) //Set an image to the 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. UIImageWriteToSavedPhotosAlbum()メソッドを使用して画像をPhotoAlbumsに保存します



// 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?)

このメソッドを呼び出すときは、このメソッドにセレクターを渡す必要があることがわかります。

例は次のとおりです。

//save into the album @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 { / / Translated into a user can understand the error reminder 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) } }