LAST CHANCE: 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) }

3      

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

3      

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
    }
  }

3      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

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.