0

I am able to mask a picture with black/white radial gradient in iOS using core graphics. But I am getting result just the opposite I want. I want the picture to be transparent from the region where gradient is applied and shall display the remaining part of the picture. That means a "![hole][1]" in the picture. But here the hole will be created by a gradient mask.

Can anyone please suggest me some way or any hint to invert the radial gradient mask, just like we do in photoshop.

4

1 に答える 1

0

マスクを使用する代わりに、レイヤーを挿入するのが最善の方法だと思います。それが私がIOSアプリに対して行ったことです。

- (void) addSublayer {
    CGPoint center = CGPointMake(self.addButton.frame.origin.x, self.addButton.frame.origin.y);
    UIBezierPath* clip = [UIBezierPath bezierPathWithArcCenter:center
                                                    radius:CGRectGetMaxX(self.addUnavailabilitySubView.frame) - self.addButton.center.x
                                                startAngle:-1.57
                                                  endAngle:1.57
                                                 clockwise:YES];
    [clip addLineToPoint:center];
    [clip closePath];

    CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
    [shapeLayer setMasksToBounds:YES];
    shapeLayer.frame = self.view.layer.bounds;
    shapeLayer.path = clip.CGPath;

    //This gradient layer can be a customised one
    CALayer *gradientLayer = [CALayer layer];
    gradientLayer.frame = CGRectMake(0, 0, self.view.bounds.size.height, self.view.bounds.size.width);

    [self.view.layer insertSublayer:gradientLayer atIndex:0];
}

ここで放射状グラデーションレイヤーを共有することもできます:)

-(void)drawInContext:(CGContextRef)ctx {
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 }; // End color

    CGFloat components[8] = {0.f, 0.f, 0.f, 0.3f, 0.f, 0.f, 0.f, 0.8f};

    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, num_locations);

    CGFloat myStartRadius, myEndRadius;

    CGPoint radialCenter = CGPointMake(100, 100)
    myStartRadius = 20;
    myEndRadius = 300;
    CGContextDrawRadialGradient (ctx, gradient, radialCenter,
                             myStartRadius, radialCenter, myEndRadius,
                             kCGGradientDrawsAfterEndLocation);
}
于 2014-01-15T13:37:56.443 に答える