Core Image several filter sample OC version number and swift version number

Source: Internet
Author: User






OC Version number





// Kaleidoscope mode
+ (CGImageRef) getKaleidoscope: (CIContext *) context
{
    CIImage * image =
    [CIImage imageWithCGImage: [UIImage imageNamed: @ "Icon"]. CGImage];
    
    CIFilter * kaleidoscope = [CIFilter filterWithName: @ "CIKaleidoscope"];
    [kaleidoscope setDefaults];
    [kaleidoscope setValue: image forKey: kCIInputImageKey];
    [kaleidoscope setValue: [CIVector vectorWithX: 5 Y: 0] forKey: kCIInputCenterKey];
    CIImage * result = [kaleidoscope valueForKey: kCIOutputImageKey];
    
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage: result fromRect: extent];
    
    return cgImage;
}

// Concave and convex deformation
+ (CGImageRef) getBumpDistortion: (CIContext *) context
{
    CIImage * image =
    [CIImage imageWithCGImage: [UIImage imageNamed: @ "Icon"]. CGImage];
    CIFilter * bumpDistortion = [CIFilter filterWithName: @ "CIBumpDistortion"];
    [bumpDistortion setDefaults];
    [bumpDistortion setValue: image forKey: kCIInputImageKey];
    [bumpDistortion setValue: [CIVector vectorWithX: 10 Y: 5] forKey: kCIInputCenterKey];
    [bumpDistortion setValue: @ 55.0f forKey: kCIInputRadiusKey];
    [bumpDistortion setValue: @ (3.0f) forKey: kCIInputScaleKey];
    CIImage * result = [bumpDistortion valueForKey: kCIOutputImageKey];
    
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage: result fromRect: extent];
    return cgImage;
}

// Hue adjustment
+ (CGImageRef) getHueAdjust: (CIContext *) context
{
    CIImage * image = [CIImage imageWithCGImage: [UIImage imageNamed: @ "Icon"]. CGImage];
    CIFilter * hueAdjust = [CIFilter filterWithName: @ "CIHueAdjust"];
    [hueAdjust setValue: image forKey: kCIInputImageKey];
    [hueAdjust setValue: @ 2.094f forKey: kCIInputAngleKey];
    CIImage * result = [hueAdjust valueForKey: kCIOutputImageKey];
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage: result fromRect: extent];
    return cgImage;
}

//darken
+ (CGImageRef) getGloom: (CIContext *) context
{
    CIImage * image = [CIImage imageWithCGImage: [UIImage imageNamed: @ "Icon"]. CGImage];
    CIFilter * gloom = [CIFilter filterWithName: @ "CIGloom"];
    [gloom setDefaults];
    [gloom setValue: image forKey: kCIInputImageKey];
    [gloom setValue: @ 5.0f forKey: kCIInputRadiusKey];
    [gloom setValue: @ 1.0f forKey: kCIInputIntensityKey];
    CIImage * result = [gloom valueForKey: kCIOutputImageKey];
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage: result fromRect: extent];
    return cgImage;
}

// Dark brown tone
+ (CGImageRef) getSepiaTone: (CIContext *) context
{
    CIImage * image = [CIImage imageWithCGImage: [UIImage imageNamed: @ "Icon"]. CGImage];
    CIFilter * sepiaTone = [CIFilter filterWithName: @ "CISepiaTone"];
    [sepiaTone setDefaults];
    [sepiaTone setValue: image forKey: kCIInputImageKey];
    [sepiaTone setValue: @ 0.8f forKey: kCIInputIntensityKey];
    CIImage * result = [sepiaTone valueForKey: kCIOutputImageKey];
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage: result fromRect: extent];
    return cgImage;
}

//synthesis
+ (CGImageRef) getAdditionCompositing: (CIContext *) context
{
    CIImage * image = [CIImage imageWithCGImage: [UIImage imageNamed: @ "Icon"]. CGImage];
    
    CIImage * image1 = [CIImage imageWithCGImage: [UIImage imageNamed: @ "ren"]. CGImage];
    
    CIFilter * accordionFoldTransition = [CIFilter filterWithName: @ "CIAdditionCompositing"];
    [accordionFoldTransition setDefaults];
    [accordionFoldTransition setValue: image forKey: kCIInputImageKey];
    [accordionFoldTransition setValue: image1 forKey: kCIInputBackgroundImageKey];
    CIImage * result = [accordionFoldTransition valueForKey: kCIOutputImageKey];
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage: result fromRect: extent];
    return cgImage;
}
// Gaussian blur
+ (CGImageRef) getGaussianBlur: (CIContext *) context
{
    CIImage * image = [CIImage imageWithCGImage: [UIImage imageNamed: @ "Icon"]. CGImage];
    CIFilter * gaussianBlur = [CIFilter filterWithName: @ "CIGaussianBlur"];
    [gaussianBlur setDefaults];
    [gaussianBlur setValue: image forKey: kCIInputImageKey];
    [gaussianBlur setValue: @ 5.0f forKey: kCIInputRadiusKey];
    
    CIImage * result = [gaussianBlur valueForKey: kCIOutputImageKey];
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage: result fromRect: extent];
    return cgImage;
}

// Zoom blur
+ (CGImageRef) getZoomBlur: (CIContext *) context
{
    CIImage * image = [CIImage imageWithCGImage: [UIImage imageNamed: @ "Icon"]. CGImage];
    CIFilter * zoomBlur = [CIFilter filterWithName: @ "CIZoomBlur"];
    [zoomBlur setDefaults];
    [zoomBlur setValue: image forKey: kCIInputImageKey];
    [zoomBlur setValue: @ 10.0f forKey: @ "inputAmount"];
    [zoomBlur setValue: [CIVector vectorWithCGPoint: CGPointMake (10, 10)] forKey: kCIInputCenterKey];
    
    CIImage * result = [zoomBlur valueForKey: kCIOutputImageKey];
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage: result fromRect: extent];
return cgImage;
}

// White point adjustment
+ (CGImageRef) getWhitePointAdjust: (CIContext *) context
{
    CIImage * image = [CIImage imageWithCGImage: [UIImage imageNamed: @ "Icon"]. CGImage];
    CIFilter * zoomBlur = [CIFilter filterWithName: @ "CIWhitePointAdjust"];
    [zoomBlur setDefaults];
    [zoomBlur setValue: image forKey: kCIInputImageKey];
    [zoomBlur setValue: [CIColor colorWithRed: 1.0 green: 0.5 blue: 1.0 alpha: 1.0] forKey: @ "inputColor"];
    
    CIImage * result = [zoomBlur valueForKey: kCIOutputImageKey];
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage: result fromRect: extent];
    return cgImage;
}

// label effect
+ (CGImageRef) getVignetteEffect: (CIContext *) context
{
    CIImage * image = [CIImage imageWithCGImage: [UIImage imageNamed: @ "Icon"]. CGImage];
    CIFilter * zoomBlur = [CIFilter filterWithName: @ "CIVignetteEffect"];
    [zoomBlur setDefaults];
    [zoomBlur setValue: image forKey: kCIInputImageKey];
    [zoomBlur setValue: [CIVector vectorWithCGPoint: CGPointMake (10, 10)] forKey: kCIInputCenterKey];
    [zoomBlur setValue: @ 0.5f forKey: @ "inputFalloff"];
    [zoomBlur setValue: @ 1.0f forKey: @ "inputIntensity"];
    [zoomBlur setValue: @ 100.0f forKey: @ "inputRadius"];
    
    CIImage * result = [zoomBlur valueForKey: kCIOutputImageKey];
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage: result fromRect: extent];
    return cgImage;
}

// Multiple effects
+ (CGImageRef) getMix: (CIContext *) context
{
    CIImage * image = [CIImage imageWithCGImage: [UIImage imageNamed: @ "Icon"]. CGImage];
    CIFilter * zoomBlur = [CIFilter filterWithName: @ "CIVignetteEffect"];
    [zoomBlur setDefaults];
    [zoomBlur setValue: image forKey: kCIInputImageKey];
    [zoomBlur setValue: [CIVector vectorWithCGPoint: CGPointMake (10, 10)] forKey: kCIInputCenterKey];
    [zoomBlur setValue: @ 0.5f forKey: @ "inputFalloff"];
    [zoomBlur setValue: @ 1.0f forKey: @ "inputIntensity"];
    [zoomBlur setValue: @ 100.0f forKey: @ "inputRadius"];
    
    CIImage * result = [zoomBlur valueForKey: kCIOutputImageKey];
    
    CIFilter * gaussianBlur = [CIFilter filterWithName: @ "CIGaussianBlur"];
    [gaussianBlur setDefaults];
    [gaussianBlur setValue: result forKey: kCIInputImageKey];
    [gaussianBlur setValue: @ 5.0f forKey: kCIInputRadiusKey];
    
    result = [gaussianBlur valueForKey: kCIOutputImageKey];
    
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage: result fromRect: extent];
    return cgImage;
}





The above describes a few filter modes, false Imagine query system built up how many kinds of filters, can run the following code to view:






// View all built-in filters


NSArray *filterNames=[CIFilterfilterNamesInCategory:kCICategoryBuiltIn];
    for (NSString *filterNamein filterNames) {
        CIFilter *filter=[CIFilterfilterWithName:filterName];
        NSLog(@"\rfilter:%@\rattributes:%@",filterName,[filterattributes]);
    }





The following intercepts some filter messages from the console output.









Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">



Take Cizoomblur filter mode for example. Print out the information. Can see that there are some attributes defined (each filter, the attributes will be different.) In use, pay special attention to).



Property names can be Kciinputimagekey using a well-defined struct member. or the direct @ "Inputimage" call, which is the picture that represents the input.



Inputimage the corresponding value defines what type this property is. Description of attributes.



Suppose you need to be able to view other filter modes.






Swift version number





// Kaleidoscope mode
    static func getKaleidoscope (context: CIContext)-> CGImageRef {
    
        let imageName = UIImage (named: "Icon")?
.CGImage
        
        let image = CIImage (CGImage: (imageName)!)
        
        let kaleidoscope = CIFilter (name: "CIKaleidoscope");
        kaleidoscope?

.setDefaults ();
        kaleidoscope? .setValue (image, forKey: kCIInputImageKey);
        kaleidoscope? .setValue (CIVector (x: 5, y: 0), forKey: kCIInputCenterKey);
        
        let result = kaleidoscope? .valueForKey (kCIOutputImageKey);
        
        let extent = result? .extent;
        
        let cgImage = context.createCGImage (result as! CIImage, fromRect: extent!)
        return cgImage
    }
    
    // Concave and convex deformation
    static func getBumpDistortion (context: CIContext)-> CGImageRef {
        let imageName = UIImage (named: "Icon")?

.CGImage
        let image = CIImage (CGImage: (imageName)!)
        
        let bumpDistortion = CIFilter (name: "CIBumpDistortion")
        bumpDistortion? .setDefaults ()
        bumpDistortion? .setValue (image, forKey: kCIInputImageKey)
        bumpDistortion?

.setValue (CIVector (x: 10, y: 5), forKey: kCIInputCenterKey)
        bumpDistortion?

.setValue (55.0, forKey: kCIInputRadiusKey)
        bumpDistortion? .setValue (3.0, forKey: kCIInputScaleKey)
        let result = bumpDistortion? .valueForKey (kCIOutputImageKey);
        
        let extent = result? .extent;
        let cgImage = context.createCGImage (result as! CIImage, fromRect: extent!)
        return cgImage
    }
    
    // Hue adjustment
    static func getHueAdjust (context: CIContext)-> CGImageRef {
        let imageName = UIImage (named: "Icon")?

.CGImage
        let image = CIImage (CGImage: (imageName)!)
        let hueAdjust = CIFilter (name: "CIHueAdjust")
        hueAdjust? .setValue (image, forKey: kCIInputImageKey)
        hueAdjust? .setValue (2.094, forKey: kCIInputAngleKey)
        
        let result = hueAdjust? .valueForKey (kCIOutputImageKey);
        
        let extent = result? .extent;
        let cgImage = context.createCGImage (result as! CIImage, fromRect: extent!)
        return cgImage
    }
    
    //darken
    static func getGloom (context: CIContext)-> CGImageRef {
        let imageName = UIImage (named: "Icon") ?. CGImage
        let image = CIImage (CGImage: (imageName)!)
        
        let gloom = CIFilter (name: "CIGloom")
        gloom? .setDefaults ()
        gloom? .setValue (image, forKey: kCIInputImageKey)
        gloom? .setValue (5.0, forKey: kCIInputRadiusKey)
        gloom? .setValue (1.0, forKey: kCIInputIntensityKey)
        
        let result = gloom? .valueForKey (kCIOutputImageKey);
        
        let extent = result? .extent;
        let cgImage = context.createCGImage (result as! CIImage, fromRect: extent!)
        return cgImage
    }
    
    // Dark brown tone
    static func getSepiaTone (context: CIContext)-> CGImageRef {
        let imageName = UIImage (named: "Icon") ?. CGImage
        let image = CIImage (CGImage: (imageName)!)
        
        let sepiaTone = CIFilter (name: "CISepiaTone")
        sepiaTone? .setDefaults ()
        sepiaTone? .setValue (image, forKey: kCIInputImageKey)
        sepiaTone? .setValue (0.8, forKey: kCIInputIntensityKey)
        
        let result = sepiaTone?

.valueForKey (kCIOutputImageKey);
        
        let extent = result?

.extent;
        let cgImage = context.createCGImage (result as! CIImage, fromRect: extent!)
        return cgImage
    }
    
    //synthesis
    static func getAdditionCompositing (context: CIContext)-> CGImageRef {
    
        let imageName = UIImage (named: "Icon") ?. CGImage
        let image = CIImage (CGImage: (imageName)!)
        let image1 = CIImage (CGImage: (UIImage (named: "ren") ?. CGImage)!)
        
        let accordionFoldTransition = CIFilter (name: "CIAdditionCompositing");
        accordionFoldTransition?

.setDefaults ()
        accordionFoldTransition? .setValue (image, forKey: kCIInputImageKey)
        accordionFoldTransition? .setValue (image1, forKey: kCIInputBackgroundImageKey)
        
        let result = accordionFoldTransition?

.valueForKey (kCIOutputImageKey);
        
        let extent = result? .extent;
        let cgImage = context.createCGImage (result as! CIImage, fromRect: extent!)
        return cgImage
    }
    
    // Gaussian blur
    static func getGaussianBlur (context: CIContext)-> CGImageRef {
        let imageName = UIImage (named: "Icon") ?. CGImage
        let image = CIImage (CGImage: (imageName)!)
        
        let gaussianBlur = CIFilter (name: "CIGaussianBlur")
        gaussianBlur? .setDefaults ()
        gaussianBlur?

.setValue (image, forKey: kCIInputImageKey)
        gaussianBlur? .setValue (5.0, forKey: kCIInputRadiusKey)
        
        let result = gaussianBlur? .valueForKey (kCIOutputImageKey);
        
        let extent = result? .extent;
        let cgImage = context.createCGImage (result as! CIImage, fromRect: extent!)
        return cgImage
    }
    
    // Zoom blur
    static func getZoomBlur (context: CIContext)-> CGImageRef {
        let imageName = UIImage (named: "Icon") ?. CGImage
        let image = CIImage (CGImage: (imageName)!)
        
        let zoomBlur = CIFilter (name: "CIZoomBlur")
        zoomBlur? .setDefaults ()
        zoomBlur?

.setValue (image, forKey: kCIInputImageKey)
        zoomBlur? .setValue (10.0, forKey: "inputAmount")
        zoomBlur? .setValue (CIVector (x: 10, y: 10), forKey: kCIInputCenterKey)
        
        let result = zoomBlur?

.valueForKey (kCIOutputImageKey);
        
        let extent = result? .extent;
        let cgImage = context.createCGImage (result as! CIImage, fromRect: extent!)
        return cgImage
    }
    
    // White point adjustment
    static func getWhitePointAdjust (context: CIContext)-> CGImageRef {
        let imageName = UIImage (named: "Icon")?

.CGImage
        let image = CIImage (CGImage: (imageName)!)
        
        let whitePointAdjust = CIFilter (name: "CIWhitePointAdjust")
        whitePointAdjust? .setDefaults ()
        whitePointAdjust? .setValue (image, forKey: kCIInputImageKey)
        whitePointAdjust? .setValue (CIColor (red: 1.0, green: 0.5, blue: 1.0, alpha: 1.0), forKey: "inputColor")
        
        let result = whitePointAdjust? .valueForKey (kCIOutputImageKey);
        
        let extent = result?

.extent;
        let cgImage = context.createCGImage (result as! CIImage, fromRect: extent!)
        return cgImage
    }
    
    // label effect
    static func getVignetteEffect (context: CIContext)-> CGImageRef {
        let imageName = UIImage (named: "Icon") ?. CGImage
        let image = CIImage (CGImage: (imageName)!)
        let vignetteEffect = CIFilter (name: "CIVignetteEffect")
        vignetteEffect? .setDefaults ()
        vignetteEffect? .setValue (image, forKey: kCIInputImageKey)
        vignetteEffect? .setValue (CIVector (x: 10, y: 10), forKey: kCIInputCenterKey)
        vignetteEffect?

.setValue (0.5, forKey: "inputFalloff")
        vignetteEffect?

.setValue (1.0, forKey: "inputIntensity")
        vignetteEffect? .setValue (100.0, forKey: "inputRadius")
        
        let result = vignetteEffect? .valueForKey (kCIOutputImageKey);
        
        let extent = result? .extent;
        let cgImage = context.createCGImage (result as! CIImage, fromRect: extent!)
        return cgImage
    }
    
    // Multiple effects
    static func getMix (context: CIContext)-> CGImageRef {
        let imageName = UIImage (named: "Icon") ?. CGImage
        let image = CIImage (CGImage: (imageName)!)
        
        let vignetteEffect = CIFilter (name: "CIVignetteEffect")
        vignetteEffect? .setDefaults ()
        vignetteEffect? .setValue (image, forKey: kCIInputImageKey)
        vignetteEffect?

.setValue (0.5, forKey: "inputFalloff")
        vignetteEffect? .setValue (1.0, forKey: "inputIntensity")
        vignetteEffect?

.setValue (100.0, forKey: "inputRadius")
        
        var result = vignetteEffect? .valueForKey (kCIOutputImageKey);
        
        let gaussianBlur = CIFilter (name: "CIGaussianBlur")
        gaussianBlur?

.setDefaults ()
        gaussianBlur? .setValue (result, forKey: kCIInputImageKey)
        gaussianBlur?

.setValue (5.0, forKey: kCIInputRadiusKey)
        
        result = gaussianBlur? .valueForKey (kCIOutputImageKey);
        
        let extent = result? .extent;
        let cgImage = context.createCGImage (result as! CIImage, fromRect: extent!)
        return cgImage
    }



The above describes a few filter modes, false Imagine query system built up how many kinds of filters. Be able to run the following code to view:






// View all built-in filters




let filterNames = CIFilter.filterNamesInCategory(kCICategoryBuiltIn)
        
        for filterName in filterNames {
            let filter = CIFilter(name: filterName)
            print("\rfilter:\(filterName)\rattributes:\(filter?.attributes)")
        }

The following intercepts some filter messages from the console output.










Take Cizoomblur filter mode, the printed information, you can see, there are some properties defined (each filter, the properties will be different.) In use, pay special attention to).



The property name can be used with a defined struct member Kciinputimagekey, or a direct "inputimage" call, which represents the input image.



Inputimage the corresponding value defines what type this property is. Description of attributes. Suppose you need to be able to view other filter modes.






project:http://download.csdn.net/detail/xiaopingping1234567/9458261


Core Image several filter sample OC version number and swift version number


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.