在 ios 中使用核心图像过滤器 - 无法保存生成的图像 - swift

Using Core Image Filters in ios - cannot save resulting image - swift

我正在对我拍摄的图像应用一些核心图像滤镜。我面临的问题是,一旦我应用了滤镜并保存了生成的图像,我就无法保存它。我的代码中没有出现任何错误,但是当我去检索图像时,应用程序无法找到它。它说该文件不存在。我怀疑我可能以某种方式错误地使用了过滤器。

 // PROCESS IMAGE TO MAKE IT LOOK 'SCANNED'
    guard let scannedImage = self.processImage(image: image)else{
        print("failed to process image")
        return
    }

    // SAVE SCANNED IMAGE TO LOCAL FILE SYSTEM
    self.saveImageToLocalFile(image: scannedImage) // PROBELM HERE.



 // PROCESS IMAGE
func processImage(image: UIImage) -> UIImage? {

    guard let ciimage = CIImage.init(image: image) else{return nil}

    // APPLY FILTERS

    guard let shadowCimage = shadow(inputImage: ciimage) else{return nil}
    guard let colorControlCimage = colorControl(input: shadowCimage, contrast: 4.0, saturation: 0.3, brightness: 0.3) else{return nil}
    guard let sharpenCimage = sharpen(inputImage: colorControlCimage, inputRadius: 2.5, inputIntensity: 1.0) else {return nil}
    guard let sharpIntensity = sharpenLumin(inputImg: sharpenCimage, inputSharpness: 0.2) else{return nil}

    print("processImage END")
    return UIImage.init(ciImage: sharpIntensity)

}

 // FILTER 'CIColorControls'
func colorControl(input: CIImage, contrast: Float, saturation: Float, brightness: Float) -> CIImage? {

    let filter = CIFilter(name: "CIColorControls")
    filter?.setValue(input, forKey: kCIInputImageKey)
    filter?.setValue(contrast, forKey: kCIInputContrastKey)
    filter?.setValue(saturation, forKey: kCIInputSaturationKey)
    filter?.setValue(brightness, forKey: kCIInputBrightnessKey)

    return filter?.outputImage
}


// FILTER 'CIUnsharpMask'
func sharpen(inputImage: CIImage, inputRadius: Float, inputIntensity: Float) -> CIImage? {

    let filter = CIFilter(name: "CIUnsharpMask")
    filter?.setValue(inputImage, forKey: kCIInputImageKey)
    //filter?.setDefaults()

    filter?.setValue(inputRadius, forKey: kCIInputRadiusKey)
    filter?.setValue(inputIntensity, forKey: kCIInputIntensityKey)

    return filter?.outputImage
}




// FILTER 'CIHighlightShadowAdjust'
func shadow(inputImage: CIImage) -> CIImage? {

    let filter = CIFilter(name: "CIHighlightShadowAdjust")
    filter?.setValue(inputImage, forKey: kCIInputImageKey)
    filter?.setDefaults()
    return filter?.outputImage
}


// FILTER 'CISharpenLuminance'
func sharpenLumin(inputImg: CIImage, inputSharpness: Float) -> CIImage? {

    let filter = CIFilter.init(name: "CISharpenLuminance")
    filter?.setValue(inputImg, forKey: kCIInputImageKey)
    filter?.setValue(inputSharpness, forKey: kCIInputSharpnessKey)
    return filter?.outputImage
}






 // SAVE UIIMAGE TO LOCAL FILE SYSTEM
func saveImageToLocalFile(image: UIImage) -> Void {

    // CREATE URL - SAVE TO PATH
    let imageURL = createPhotoURL() // CORRECT FULL LENGTH URL FOR FILE UPLAOD
    print("IMAGE SAVED TO URL: \(imageURL)")
    let imageData = UIImageJPEGRepresentation(image, 1.0)
    do{

        try imageData?.write(to: imageURL)
        self.scannedImageURL = imageURL

    }catch{
        print("error writing img to local dir")
    }
}

问题是您的 imageDatanil,因为您创建 UIImage 的方式。

中所述,您在 UIImage.cgImage 属性 中有一个 nilUIImageJPEGRepresentation 使用。这是您需要使用的代码:

func saveImageToLocalFile(image: UIImage) -> Void {
    let imageURL = createPhotoURL()
    var uiImage = image
    if image.cgImage == nil {
        guard let ciImage = image.ciImage, let cgImage = CIContext(options: nil).createCGImage(ciImage, from: ciImage.extent) else { return }
        uiImage = UIImage(cgImage: cgImage)
    }
    if let imageData = UIImageJPEGRepresentation(uiImage, 1.0) {
        do {
            try imageData?.write(to: imageURL)
            self.scannedImageURL = imageURL
        } catch {
            print("error writing img to local dir")
        }
    } else {
            print("could not create JPEG image")
    }
}