Search code examples
iphoneiosios6

How to use CIStarShineGenerator?


I'm trying out the new CIFilter, CIStarShineGenerator.

But the output Image is just blank, how could I check if its generated normally?

CIColor *blue = [CIColor colorWithString:@"0.1 0.5 0.8 1.0"];
CIFilter *filter = [CIFilter filterWithName:@"CIStarShineGenerator" keysAndValues:@"inputColor", blue, nil];
CIImage *outputImage = [filter outputImage];
UIImage *image = [UIImage imageWithCIImage:outputImage];

_imageView.image = image;

imageView is an Image View which shows at the screen.


Solution

  • I found this filter on Apple's site and figured out it would be perfect for something I'm trying to do but there's almost no information on it, and this question is the only reference to it on SO I could find.

    So after messing with it for a long time, I got it working.

    Basically you were on the right trail but there's more values to be set, and you have to create a UIImage with a particular CGRect sufficient to hold the end result.

    The following would probably achieve what you were trying to do, with a star that's 10x10 pixels (size is basically the radius).

    CGFloat size = 5;
    
    CIFilter *filter = [CIFilter filterWithName:@"CIStarShineGenerator"];
    CIColor *blue = [[CIColor alloc] initWithColor:[UIColor colorBlue]];
    
    [filter setDefaults];
    [filter setValue:[NSNumber numberWithFloat:size/2] forKey:@"inputRadius"];
    [filter setValue:[NSNumber numberWithFloat:1] forKey:@"inputCrossWidth"];
    [filter setValue:[NSNumber numberWithFloat:2] forKey:@"inputCrossScale"];
    [filter setValue:blue forKey:@"inputColor"];
    [filter setValue:[CIVector vectorWithX:size Y:size] forKey:@"inputCenter"];
    
    CIImage *outputImage = [filter valueForKey:@"outputImage"];
    
    CIContext *context = [CIContext contextWithOptions:nil];
    
    CGFloat UIImageOriginX = 0;
    CGFloat UIImageOriginY = 0;
    CGFloat UIImageSizeWidth = size * 2;
    CGFloat UIImageSizeHeight  = size * 2;
    
    UIImage *image = [UIImage imageWithCGImage:[context createCGImage:outputImage fromRect:CGRectMake(UIImageOriginX, UIImageOriginY, UIImageSizeWidth, UIImageSizeHeight)]];
    
    _imageView.image = image;
    

    Some of the above is overkill (you could just define the CGRect sizes inline) but I was trying to mess with various variables in a clear manner.