使用过滤器将图像保存到Swift 3.0中的照片库

时间:2016-10-02 15:55:52

标签: swift ios10 core-image cifilter

我在Swift中使用Core Image编辑照片,我在保存照片时遇到问题。将滤镜应用于照片后,无法保存。但是没有过滤器,它可以保存。有什么想法吗?

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var pickedImaged: UIImageView!


override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}


@IBAction func camerabuttonaction(_ sender: UIButton) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
    let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
        imagePicker.allowsEditing = false
        self.present(imagePicker, animated: true, completion: nil)
    }
}


@IBAction func photolibraryaction(_ sender: UIButton) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary;
        imagePicker.allowsEditing = false
        self.present(imagePicker, animated: true, completion: nil)
    }
}


@IBAction func saveaction(_ sender: UIButton) {
UIImageWriteToSavedPhotosAlbum(pickedImaged.image!, nil, nil, nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]) {   
    pickedImaged.image = image
    self.dismiss(animated: true, completion: nil);     
}


@IBAction func addfilter(_ sender: UIButton) {

    let image = pickedImaged.image, cgimg = image?.cgImage
    let coreImage = CIImage(cgImage: cgimg!)      
    let filter = CIFilter(name: "CISepiaTone")
    filter?.setValue(coreImage, forKey: kCIInputImageKey)
    filter?.setValue(0.5, forKey: kCIInputIntensityKey)     
    let output = filter?.value(forKey: kCIOutputImageKey) as? CIImage
    let filteredImage = UIImage(ciImage: output!)
    pickedImaged.image = filteredImage       
}


}

1 个答案:

答案 0 :(得分:1)

我的代码适用于iOS 9& iOS 10:

(1)我使用iOS共享扩展,它总是让你能够复制到剪贴板和"保存"功能,以及消息,邮件等。代码如下。

(2)如果没有看到照片编辑代码,我可能会说明显而易见的。但是 - 确保您(a)正在为物品提供CI过滤器输出,并且(b)正确地为过滤器提供所需的参数。

我有某些类型的"股票"注释掉了,但这就是我的所作所为:

let context = CIContext()
let final = context.createCGImage(filter.outputImage, from: filter.outputImage.extent)
let shareImage = UIImage(cgImage: final!)
let vc = UIActivityViewController(activityItems: [shareImage], applicationActivities: [])
vc.excludedActivityTypes =  [
    //UIActivityTypePostToTwitter,
    //UIActivityTypePostToFacebook,
    UIActivityType.postToWeibo,
    //UIActivityTypeMessage,
    //UIActivityTypeMail,
    UIActivityType.print,
    //UIActivityTypeCopyToPasteboard,
    UIActivityType.assignToContact,
    //UIActivityTypeSaveToCameraRoll,
    UIActivityType.addToReadingList,
    //UIActivityTypePostToFlickr,
    UIActivityType.postToVimeo,
    UIActivityType.postToTencentWeibo
]
present(vc,
        animated: true,
        completion: nil)
vc.popoverPresentationController?.sourceView = self.view
vc.completionWithItemsHandler = {(activity, success, items, error) in
}