xcode 5在一个Image(UIImageView)上应用多个过滤器(iOS7.1)

时间:2014-03-27 17:34:28

标签: objective-c image filter core-image

我有一个TabBar,我可以选择我想要使用的过滤器。如果我选择一个Slider,我可以使用过滤器。但如果我选择其他滤镜,图像将重置为原始。我怎么能在xcode 5中说所有过滤器都应该保留?

抱歉我的英语不好。我来自德国。

我的过滤器代码:

int b, i;

// Select first tab by default
tabBar.selectedItem = [tabBar.items objectAtIndex:0];

// Create a bitmap context for rendering the tabBar buttons
// Usually, button images are loaded from disk, but these simple shapes can be procedurally generated.
// UITabBar only needs the alpha channel of these images.
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(nil, 30, 30, 6, 0, colorSpace, kCGImageAlphaPremultipliedFirst);
CGImageRef theCGImage;

// Draw with white round strokes
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(context, 2.0);

for (b = 0; b < NUM_BUTTONS; b++)
{
    CGContextClearRect(context, CGRectMake(0, 0, 30, 30));

    switch(b)
    {
        case BUTTON_BRIGHTNESS:
        {
            const CGFloat line[8*4] = {
                15.0, 6.0, 15.0, 4.0,
                15.0,24.0, 15.0,26.0,
                6.0,15.0,  4.0,15.0,
                24.0,15.0, 26.0,15.0,
                21.5,21.5, 23.0,23.0,
                8.5, 8.5,  7.0, 7.0,
                21.5, 8.5, 23.0, 7.0,
                8.5,21.5,  7.0,23.0,
            };

            // A circle with eight rays around it
            CGContextStrokeEllipseInRect(context, CGRectMake(10.5, 10.5, 9.0, 9.0));
            for (i = 0; i < 8; i++)
            {
                CGContextMoveToPoint(context, line[i*4+0], line[i*4+1]);
                CGContextAddLineToPoint(context, line[i*4+2], line[i*4+3]);
                CGContextStrokePath(context);
            }
            break;
            [BUTTON_BRIGHTNESS setValue: forKey:@"inputImage"];

        }
        case BUTTON_CONTRAST:
        {
            // A circle with the right half filled
            CGContextStrokeEllipseInRect(context, CGRectMake(4.0, 4.0, 22.0, 22.0));
            CGContextAddArc(context, 15.0, 15.0, 11.0, -M_PI/2.0, M_PI/2.0, false);
            CGContextFillPath(context);
            break;
        }
        case BUTTON_SATURATION:
        {
            CGGradientRef gradient;
            const CGFloat stripe[3][12] = {
                { 0.3,0.3,0.3,0.15, 1.0,0.0,0.0,0.70,  5, 5, 7, 25 },
                { 0.5,0.5,0.5,0.25, 0.0,1.0,0.0,0.75, 12, 5, 6, 25 },
                { 0.2,0.2,0.2,0.10, 0.0,0.0,1.0,0.65, 18, 5, 7, 25 },
            };

            // Red/Green/Blue gradients, inside a rounded rect
            for (i = 0; i < 3; i++)
            {
                gradient = CGGradientCreateWithColorComponents(colorSpace, stripe[i], NULL, 2);
                CGContextSaveGState(context);
                CGContextClipToRect(context, CGRectMake(stripe[i][8], stripe[i][9], stripe[i][10], stripe[i][11]));
                CGContextDrawLinearGradient(context, gradient, CGPointMake(15, 5), CGPointMake(15, 25), 0);
                CGContextRestoreGState(context);
                CGGradientRelease(gradient);
            }

            CGContextMoveToPoint(context, 4, 15);
            CGContextAddArcToPoint(context, 4, 4, 15, 4, 4);
            CGContextAddArcToPoint(context, 26, 4, 26, 15, 4);
            CGContextAddArcToPoint(context, 26, 26, 15, 26, 4);
            CGContextAddArcToPoint(context, 4, 26, 4, 15, 4);
            CGContextClosePath(context);
            CGContextStrokePath(context);
            break;
        }
        case BUTTON_HUE:
        {
            CGGradientRef gradient;
            CGFloat hue[8];
            const int angle = 4;

            // A radial gradient, inside a circle
            for (i = 0; i < 360; i+=angle)
            {
                float x = cosf((i+angle*0.5)*DEG2RAD)*10+15;
                float y = sinf((i+angle*0.5)*DEG2RAD)*10+15;
                float r = (i    )/180.0; if (r > 1.0) r = 2.0-r;
                float g = (i+120)/180.0; if (g > 2.0) g = g-2.0; else if (g > 1.0) g = 2.0-g;
                float b = (i+240)/180.0; if (b > 3.0) b = 4.0-b; else if (b > 2.0) b = b-2.0; else b = 2.0-b;
                float a = (i+ 90)/180.0; if (a > 2.0) a = a-2.0; else if (a > 1.0) a = 2.0-a;
                hue[0] = hue[4] = r;
                hue[1] = hue[5] = g;
                hue[2] = hue[6] = b;
                hue[3] = a*0.5;
                hue[7] = a*0.75;

                gradient = CGGradientCreateWithColorComponents(colorSpace, hue, NULL, 2);
                CGContextSaveGState(context);
                CGContextMoveToPoint(context, 15, 15);
                CGContextAddArc(context, 15, 15, 10, i*DEG2RAD, (i+angle)*DEG2RAD, false);
                CGContextClosePath(context);
                CGContextClip(context);
                CGContextDrawLinearGradient(context, gradient, CGPointMake(x, y), CGPointMake(15, 15), 0);
                CGContextRestoreGState(context);
                CGGradientRelease(gradient);
            }

            CGContextStrokeEllipseInRect(context, CGRectMake(4.0, 4.0, 22.0, 22.0));
            break;
        }
        case BUTTON_SHARPNESS:
        {
            int x, y;

            // A gradient checkerboard, inside a rounded rect
            for (x = 5; x < 25; x+=2)
            {
                float b = (x - 5)/19.0*0.5+0.375;
                if (b > 0.75) b = 0.75;
                else if (b < 0.5) b = 0.5;

                for (y = 5; y < 25; y+=2)
                {
                    float k = ((x ^ y) & 2) ? b : 1.0-b;
                    CGContextSetRGBFillColor(context, k, k, k, k);
                    CGContextFillRect(context, CGRectMake(x, y, 2, 2));
                }
            }

            CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
            CGContextMoveToPoint(context, 4, 15);
            CGContextAddArcToPoint(context, 4, 4, 15, 4, 4);
            CGContextAddArcToPoint(context, 26, 4, 26, 15, 4);
            CGContextAddArcToPoint(context, 26, 26, 15, 26, 4);
            CGContextAddArcToPoint(context, 4, 26, 4, 15, 4);
            CGContextClosePath(context);
            CGContextStrokePath(context);
            break;
        }
    }
    theCGImage = CGBitmapContextCreateImage(context);
    ((UITabBarItem *)[tabBar.items objectAtIndex:b]).image = [UIImage imageWithCGImage:theCGImage];
    CGImageRelease(theCGImage);
}

CGContextRelease(context);
CGColorSpaceRelease(colorSpace);

}

1 个答案:

答案 0 :(得分:-3)

使用GPUImage过滤器更容易。尝试设置这些,而不是创建另一个问题。