views:

94

answers:

1

Hi,

We have an issue with transparency. While writing an image to Context with gradient, transparency (which is unwanted) is getting applied. We are not sure why this has been getting applied. We need the context to be "ONLY" with Gradient but not with "TRANSPARENCY".

Attaching the snippet of the code for your reference.

- (UIImage *)ReflectImage:(CGFloat)refFract {
    int reflectionHeight = self.size.height * refFract;

    CGImageRef gradientMaskImage = NULL;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

    CGContextRef gradientBitmapContext = CGBitmapContextCreate(nil, 1, reflectionHeight,
                                                               8, 0, colorSpace, kCGImageAlphaNone);

    CGFloat colors[] = {0.0, 1.0, 1.0, 1.0};

    CGGradientRef grayScaleGradient = CGGradientCreateWithColorComponents(colorSpace, colors, NULL, 2);
    CGColorSpaceRelease(colorSpace);

    CGPoint gradientStartPoint = CGPointMake(0, reflectionHeight);
    CGPoint gradientEndPoint = CGPointZero;

    CGContextDrawLinearGradient(gradientBitmapContext, grayScaleGradient, gradientStartPoint,
                                gradientEndPoint, kCGGradientDrawsAfterEndLocation);
    CGGradientRelease(grayScaleGradient);

    CGContextSetGrayFillColor(gradientBitmapContext, 0.0, 0.5);
    CGContextFillRect(gradientBitmapContext, CGRectMake(0, 0, 1, reflectionHeight));

    gradientMaskImage = CGBitmapContextCreateImage(gradientBitmapContext);
    CGContextRelease(gradientBitmapContext);

    CGImageRef reflectionImage = CGImageCreateWithMask(self.CGImage, gradientMaskImage);
    CGImageRelease(gradientMaskImage);

    CGSize size = CGSizeMake(self.size.width, self.size.height + reflectionHeight);

    UIGraphicsBeginImageContext(size);

    [self drawAtPoint:CGPointZero];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, CGRectMake(0, self.size.height, self.size.width, reflectionHeight), reflectionImage);

    UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRelease(reflectionImage);

    return result;
}

So, can someone please let me know why this is so? It would be of great help if this issue gets resolved.

Thanks!!

A: 

I didn't try running any of this, but you do seem to be passing an alpha value to CGContextSetGrayFillColor.

Also, the use of "device gray" has been generally discouraged. You might want to double check to make sure that the color space you're getting back has the same number of components as you expect it to.

Azeem.Butt