how to properly apply Core Image filter
Asked Answered
L

2

5

Im having a trouble with Core image. what I'm doing is getting an image from a UIImageView and then use some code i found in tutorials (I'm new to core Image) but then I want to put the sepia image back into the same same UIImageView when ever I try to put the new image into the view it just disappears I have tested to see if the image view contains an image and it does but it is not visible. any suggestions on what to do?

EDIT: okay I got the sepia filter to work so then I tried posterize and i had the same problem the image just disappears. Here is the code:

CIImage *beginImage = [CIImage imageWithCGImage:[image_view.image CGImage]];
context = [CIContext contextWithOptions:nil];
filter = [CIFilter filterWithName:@"CIColorPosterize" keysAndValues:kCIInputImageKey, beginImage,@"inputLevels",[NSNumber numberWithFloat:0.8], nil];
CIImage *outputImage = [filter outputImage];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *newImg = [UIImage imageWithCGImage:cgimg];
[image_view setImage:newImg];
CGImageRelease(cgimg);
Lam answered 22/6, 2012 at 0:15 Comment(2)
Post the code you have thus far!!!Tracery
There are CoreImage filters missing from the iOS SDK. I believe posterize is one of them. Google for iOS supported CoreImage filters for the list of supported ones.Mold
C
11

Try something like this (let say you have an UIImageView *myImageView)

CIImage *beginImage = [CIImage imageWithCGImage:[myImageView.image CGImage]];
CIContext *context = [CIContext contextWithOptions:nil];

CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone" keysAndValues: kCIInputImageKey, beginImage, @"inputIntensity", [NSNumber numberWithFloat:0.8], nil];
CIImage *outputImage = [filter outputImage];

CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *newImg = [UIImage imageWithCGImage:cgimg];

[myImageView setImage:newImg];

CGImageRelease(cgimg);
Chassepot answered 22/6, 2012 at 0:28 Comment(2)
The source for this answer: raywenderlich.com/22167/beginning-core-image-in-ios-6Virga
Or this: blog.objectgraph.com/index.php/2011/12/31/… or this: books.google.com/… or this technet.weblineindia.com/mobile/… or ...Chassepot
Z
0

Same code in Swift 3.0 on Xcode 8.2 (My imageView in project is "@IBOutlet var photoImageView : UIImageView!") Checked successfully on an iPhone 7Plus device iOS 10

    guard let myImageView = self.photoImageView.image else
    {
        return
    }
    guard let cg = myImageView.cgImage else
    {
        return
    }
    let beginImage = CIImage(cgImage: cg)
    let context : CIContext = CIContext(options: nil)
    let inputParams : [String : Any]? = [kCIInputImageKey : beginImage,
                                        "inputIntensity" : NSNumber(value : 0.8)]
    let filter : CIFilter = CIFilter(name: "CISepiaTone", withInputParameters: inputParams)!
    let outputImage : CIImage? = filter.outputImage!

    if let outputImage = outputImage
    {
        let cgImage : CGImage? = context.createCGImage(outputImage, from: outputImage.extent)!

        if let cg = cgImage
        {
            let newImage : UIImage = UIImage(cgImage: cg)
            photoImageView.image = newImage
        }
    }
Zavras answered 16/2, 2017 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.