透明度问题

时间:2009-12-16 20:04:34

标签: iphone views transparency gradient

我们有透明度问题。在使用渐变将图像写入Context时,将应用透明度(不需要的)。我们不确定为什么要这样做。我们需要上下文与渐变“仅”,而不是“透明度”。

附上代码片段供您参考。

- (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;
}

那么,有人可以让我知道为什么会这样吗?如果这个问题得到解决将会有很大的帮助。

谢谢!

1 个答案:

答案 0 :(得分:0)

我没有尝试运行任何此类操作,但您似乎确实将alpha值传递给了CGContextSetGrayFillColor。

此外,一般不鼓励使用“设备灰色”。您可能需要仔细检查以确保您获得的色彩空间具有与预期相同数量的组件。