0

This project is taken from SwiftOCR by garnele007. There is a NSImage (That is also converted into a .png for saving)in the Swift project that I would like to add a shadow on top of. Several examples of adding a shadow inside the viewcontroller etc exist but so far an example of how to use something like Core Graphics/Image to add a shadow has eluded me. My imagination gives me something like

randomImg.CGAddShadow(shadow: true, weight: medium)

A few of the instructions floating around use UIKit which won't work for this MacOS project. Others add a temporary layer for the benefit of the user but don't save the file with the shadow still applied.

The code currently looks like this:


            let customImage: (String) -> OCRImage = { code in
                let randomImg = randomImage()
                randomImg.lockFocus()

                randomImg.draw(in: CGRect(origin: CGPoint.zero, size: randomImg.size))


                NSString(string: code).draw(in: CGRect(origin: CGPoint(x: 0, y: -15.5 ), size: randomImg.size), withAttributes: randomFontAttributes())

                randomImg.unlockFocus()


                let data = NSBitmapImageRep(data: randomImg.tiffRepresentation!)?.representation(using: .png, properties: [:])

                 return randomImg
            }

The end result is hopefully a modified file with a shadow over it that can be saved and viewed later.

Thanks for any suggestions

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Morssel
  • 38
  • 8

1 Answers1

0

The solution I ended up using (A random Sepia filter):


            let context = CIContext()

            // Load originalCIImage with contents of the URL
            var imageURL = Bundle.main.urlForImageResource(NSImage.Name(rawValue: "image.png"))
            var originalCIImage = CIImage(contentsOf: imageURL!)

            // Declare func for filter
            func sepiaFilter(_ input: CIImage, intensity: Double) -> CIImage?
            {
                let sepiaFilter = CIFilter(name:"CISepiaTone")
                sepiaFilter?.setValue(input, forKey: kCIInputImageKey)
                sepiaFilter?.setValue(intensity, forKey: kCIInputIntensityKey)
                return sepiaFilter?.outputImage
            }

            // call the filter func on the image
            let sepiaCIImage = sepiaFilter(originalCIImage!, intensity:0.9)

            // change the image from CIImage type to NSImage type
            let rep = NSCIImageRep(ciImage: sepiaCIImage!)
            let filteredImage = NSImage(size: rep.size)
            filteredImage.addRepresentation(rep)

            // change from NSImage type to .png format
            let filterData = NSBitmapImageRep(data: filteredImage.tiffRepresentation!)?.representation(using: .png, properties: [:])

            // save the png file to the desktop
            do {
                try filterData?.write(to: (FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first?.appendingPathComponent("FILTEREDIMAGE.png"))!)
            }
            catch _ {
                print("Adding filter to picture error")
            }

// END Sepia Filter
Morssel
  • 38
  • 8