Add Dark and Highlight effect on image Like apple native app Dark and Highlight effect - swift

Hi Every one I am facing issue regarding to implement Dark and Highlight effect using Core Image builtin filters. I have worked with other filters but this one giving me wrong result or may be I am not using right filter. I am using CIFilter.highlightShadowAdjust filter Here is my implementation.
let context = CIContext(options: nil)
let filter = CIFilter.highlightShadowAdjust()
let aCIImage = CIImage(image: self.image)
filter.setValue(aCIImage, forKey: kCIInputImageKey)
filter.setValue(highlitValue, forKey: "inputHighlightAmount")
filter.setValue(shadowValue, forKey: "inputShadowAmount")
filter.setValue(radiousValue, forKey: "inputRadius")
guard let outputImage = filter.outputImage else {return UIImage()}
guard let cgimg = context.createCGImage(outputImage, from: outputImage.extent) else {return UIImage()}
return UIImage(cgImage: cgimg)
For clarification I also uploaded a Video what I want to achieve.
Any Suggestion or guid will be greatly thankful.

Related

Swift, blurred images in UITableViewCell does not run smooth

what i am trying to do is show a list with blurred images from the web. This works fine with this code in my custom UITableViewCell
func blurImage(image:UIImage, imageView: UIImageView) {
DispatchQueue.global(qos: .background).async {
let inputImage = CIImage(image: image)
let originalOrientation = image.imageOrientation
let originalScale = image.scale
let filter = CIFilter(name: "CIGaussianBlur")
filter?.setValue(inputImage, forKey: kCIInputImageKey)
filter?.setValue(15.0, forKey: kCIInputRadiusKey)
let outputImage = filter?.outputImage
var cgImage:CGImage?
if let outputImage = outputImage{
cgImage = self.context.createCGImage(outputImage, from: (inputImage?.extent)!)
}
DispatchQueue.main.async {
if let cgImageA = cgImage{
imageView.image = UIImage(cgImage: cgImageA, scale: originalScale, orientation: originalOrientation)
}
}
}
}
the problem is that the blur calculation takes sometime, and allthought its on a BG thread the scrolling is not that fast and smooth as if i don't have the blur effect at all.
Is there a way to make it run smoother OR to show a placeholder image until the blurred image is ready to been draw again resulting in smooth scrolling?
Step 1) Don't use qos: .background for user-initiated tasks. Docs say: Background tasks have the lowest priority of all tasks. Assign this class to tasks or dispatch queues that you use to perform work while your app is running in the background.

Is the a way to modify the squares in the corners by circles in a QR?

I have followed this tutorial (https://medium.com/#dominicfholmes/generating-qr-codes-in-swift-4-b5dacc75727c) to generate qr, but I am trying to generate customized qr and one of the requirements is that instead of being squares they are circles in the corners. This is possible?
func generateQR(fromString : String) -> UIImage? {
let data = fromString.data(using: String.Encoding.ascii)
// Get a QR CIFilter
guard let qrFilter = CIFilter(name: "CIQRCodeGenerator") else { return nil}
// Input the data
qrFilter.setValue(data, forKey: "inputMessage")
// Get the output image
guard let qrImage = qrFilter.outputImage else { return nil}
// Scale the image
let transform = CGAffineTransform(scaleX: 10, y: 10)
let scaledQrImage = qrImage.transformed(by: transform)
// Invert the colors
guard let colorInvertFilter = CIFilter(name: "CIColorInvert") else { return nil}
colorInvertFilter.setValue(scaledQrImage, forKey: "inputImage")
guard let outputInvertedImage = colorInvertFilter.outputImage else { return nil}
// Replace the black with transparency
guard let maskToAlphaFilter = CIFilter(name: "CIMaskToAlpha") else { return nil}
maskToAlphaFilter.setValue(outputInvertedImage, forKey: "inputImage")
guard let outputCIImage = maskToAlphaFilter.outputImage else { return nil}
// Do some processing to get the UIImage
let context = CIContext()
guard let cgImage = context.createCGImage(outputCIImage, from: outputCIImage.extent) else { return nil}
let processedImage = UIImage(cgImage: cgImage)
return processedImage
}
There is an example of expected result
https://www.qrcode-monkey.com/img/qrcode-logo.png
It's been a while since I've used the Core Image QR code generator filter, CIQRCodeGenerator. Looking at the docs, it only takes a couple of parameters, inputMessage and inputCorrectionLevel. There's no facility other than those parameters to customize the QR code it generates.
I guess you could do image processing on the resulting image to find the "bullseye" corner squares to change them to rounded rectangles, but that would be a fair challenge.
Conversely you could always write your own QR code rendering library. The image processing part isn't that complicated. It's figuring out the QR code standard and how to generate the dot pattern that would be hard. I haven't looked up the specs for QR codes but it's public.
You might take an existing open source QR code library and modify it to create the rounded rectangle corner squares you are after. I think this is the option I would pursue if it was my task. With any luck you can find a well-written library that first generates the QR code as a grid of booleans, and then uses a separate function to render that grid of boons into an image.

CIFIlter Apply to animationImages of UIImageView

Using CIFIlter I want to apply same filter to multiple images
I have multiple animationImages of UIImageView
let sepiaFilter = CIFilter(name:"CIColorControls")
let brightness = 0.8
for image in imageView.animationImages {
guard let ciimage = CIImage(image: image) else { return }
if let newimage = self.sepiaFilter(ciimage, filter: filter, intensity:brightness )
{
let cgImage:CGImage = ciImageCtx!.createCGImage(newimage, from: newimage.extent)!
let image:UIImage = UIImage.init(cgImage: cgImage)
newImages.append(image)
}
}
}
func sepiaFilter(_ input: CIImage,filter: CIFilter?, intensity: Double) -> CIImage?
{
filter?.setValue(input, forKey: kCIInputImageKey)
filter?.setValue(intensity, forKey: kCIInputBrightnessKey)
return filter?.outputImage
}
So let me know what is best solution to apply CIFilter to multiple images ?
Using above for loop CPU Usage increased more than 100% so it is totally wrong way.
Is it possible animations in GLKit View ?
If yes let me provide deatils about it or Give best solution
**let cgImage:CGImage = ciImageCtx!.createCGImage(newimage, from: newimage.extent)!**
This line taking more CPU usage and time
Thanks.

Applying a CIFilter with SKEffectNode to a SKSpriteNode

I am trying to apply a CIFilter with SKEffectNode to a SKSpriteNode.
Can't get this to work and not sure why. I got this working using a blur but cannot get the mono photo effect.
Anyone have any insight? Thanks again.
//Mono Effect (not working)
let filter = CIFilter(name: "CIPhotoEffectMono")
filter?.setDefaults()
effectsNode.filter = filter
self.addChild(effectsNode)
effectsNode.addChild(SKSpriteNode)
//Blur Effect (working)
let filter = CIFilter(name: "CIGaussianBlur")
let blurAmount = 4.0
filter?.setValue(blurAmount, forKey: kCIInputRadiusKey)
effectsNode.filter = filter
effectsNode.blendMode = .alpha
self.addChild(effectsNode)
effectNode.addChild(SKSpriteNode)

Apply CiFilter GaussianBlur

As the title says I need to implement GaussianBlur to an UIImage; i tried to search for a tutorial but I am not still able to implement it. I tried this
var imageToBlur = CIImage(image: coro.logo)
var blurfilter = CIFilter(name: "CIGaussianBlur")
blurfilter.setValue(imageToBlur, forKey: "inputImage")
blurfilter.setValue(2, forKey: "inputImage")
var resultImage = blurfilter.valueForKey("outputImage") as! CIImage
var blurredImage = UIImage(CIImage: resultImage)
self.immagineCoro.image = blurredImage
importing CoreImage framework, but Xcode shows me an error ("NSInvalidArgumentException") at line 5. Can anyone help me to implement gaussianBlur and CIFilter in general?
Edit: thank to you both, but I have an other question; I need to apply blur only to a little part of the image like this
I just tried your code, and here's the modification I suggest, this works:
let fileURL = NSBundle.mainBundle().URLForResource("th", withExtension: "png")
let beginImage = CIImage(contentsOfURL: fileURL)
var blurfilter = CIFilter(name: "CIGaussianBlur")
blurfilter.setValue(beginImage, forKey: "inputImage")
//blurfilter.setValue(2, forKey: "inputImage")
var resultImage = blurfilter.valueForKey("outputImage") as! CIImage
var blurredImage = UIImage(CIImage: resultImage)
self.profileImageView.image = blurredImage
So, commenting out the portion you see above, did the trick and I get a blurred image as expected. And I'm using the file path, but this shouldn't make a difference from what you have.
You've used inputImage twice. The second time is probably meant to be inputRadius.
You might want to create a CIImage greyscale mask image with the shape you want, a blurred CIImage (using CIGaussianBlur), and then use CIBlendWithMask to blend them together.
The inputs of CIBlendWithMask are the input image (the blurred image), the input background image (the unblurred image), and the mask image (the shape you want). The output is the image you desire.