如何在Swift IOS中将图像CMY转换为半色调?

时间:2018-12-19 07:06:56

标签: ios swift4

如何在Swift IOS中将图像CMY转换为半色调?

1 个答案:

答案 0 :(得分:0)

这是我使用很久了

调用将简单图像转换为CICMYKHalftone图像的函数-

let myConvertdImage = simpleCMYKHalftoneFilterMethod(inputImage: ImageView.image!)

和实际功能看起来像这样-

func simpleCMYKHalftoneFilterMethod(inputImage: UIImage) -> UIImage {
        // Create and apply filter
        // 1 - create source image
        let sourceImage = CIImage(image: inputImage)

        // 2 - create filter using name   CICMYKHalftone   CIPhotoEffectNoir
        let myFilter = CIFilter(name:"CICMYKHalftone")

        // 3 - set source image
        myFilter?.setValue(sourceImage, forKey: kCIInputImageKey)

        // 4 - create core image context
        let context = CIContext(options: nil)

        // 5 - output filtered image as cgImage with dimension.
        let outputCGImage = context.createCGImage(myFilter!.outputImage!, from: myFilter!.outputImage!.extent)

        // 6 - convert filtered CGImage to UIImage
        let filteredImage = UIImage(cgImage: outputCGImage!)

        // 7 - set filtered image to preview
        self.ImageView.image = filteredImage

        //image base64 string
        let userImage:UIImage = self.ImageView.image!
        let imageData:NSData = UIImagePNGRepresentation(userImage)! as NSData
        imageBase64Str = imageData.base64EncodedString(options: .lineLength64Characters)

        return filteredImage
    }

希望这对您有所帮助。编码愉快:)