BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Day 67 help needed with colorPosterize()

Forums > 100 Days of SwiftUI

I was trying to modify the slidervalue used in the applyProcessing() function when the filter is colorPosterize.

The issue I am finding is there does not seem to be a matching kCIInput key that matches levels the docs say it needs an NSNumber and the name is Levels. Similar to how Sepia uses Intensity

So I thought there would be a kCIInputLevelsKey but that doesnt work...

Can anyone help?

   //code below works
   if inputKeys.contains(kCIInputScaleKey) && (currentFilter.name == "CIPixellate") {
      print("this is pixellate")
      currentFilter.setValue(scaleIntensity * 75, forKey: kCIInputScaleKey) }

    //code below doesnt run as I do not think the Contains is finding the correct kCI key

    if inputKeys.contains(kCIInputIntensityKey) && (currentFilter.name == "CIColorPosterize") {
        print("this is the posterize effect")
      currentFilter.setValue(filterIntensity * 10, forKey: kCIInputIntensityKey) }

   

Instead of using a constant, try using the string "inputLevels".

   

That worked perfect! I actually tried a string but I think I put in Levels instead of inputLevels... Here is my code which works awesome!

Thank you so much @roosterboy !!

 func applyProcessing() {
    print(currentFilter.name)
    let inputKeys = currentFilter.inputKeys
    print(inputKeys)

    if inputKeys.contains(kCIInputIntensityKey) {
      currentFilter.setValue(filterIntensity, forKey: kCIInputIntensityKey) }
    if inputKeys.contains(kCIInputRadiusKey) {
      currentFilter.setValue(filterIntensity * 200, forKey: kCIInputRadiusKey) }
    if inputKeys.contains(kCIInputScaleKey) {
      currentFilter.setValue(scaleIntensity * 10, forKey: kCIInputScaleKey) }
    if inputKeys.contains(kCIInputScaleKey) && (currentFilter.name == "CIPixellate") {
      print("this is pixellate")
      currentFilter.setValue(scaleIntensity * 75, forKey: kCIInputScaleKey) }
    if inputKeys.contains("inputLevels") && (currentFilter.name == "CIColorPosterize") {
        print("this is the posterize effect")
      currentFilter.setValue(filterIntensity * 30, forKey: "inputLevels")
    }

    guard let outputImage = currentFilter.outputImage else { return }

    if let cgimg = context.createCGImage(outputImage, from: outputImage.extent) {
      let uiImage = UIImage(cgImage: cgimg)
      image = Image(uiImage: uiImage)
      processedImage = uiImage
    }
  }

   

Hacking with Swift is sponsored by Guardsquare

SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.