Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios CGGradientCreateWithColorComponents和alpha通道I_Ios_Core Graphics - Fatal编程技术网

Ios CGGradientCreateWithColorComponents和alpha通道I

Ios CGGradientCreateWithColorComponents和alpha通道I,ios,core-graphics,Ios,Core Graphics,我正在创建CGGradientRef,用它来支持alpha通道: 此数组中的项数应为计数和 颜色空间中的组件数。例如,如果颜色 空间是一个RGBA颜色空间,您希望在空间中使用两种颜色 渐变(一个用于起始位置,另一个用于结束位置 位置),然后您需要在组件中提供8个值(红色、绿色、, 蓝色和第一种颜色的alpha值,然后是红色、绿色, 蓝色和第二种颜色的alpha值 以下是完整的视图实现: h m 实现AlphaGrad -(id)initWithFrame:(CGRect)帧 { self=[su

我正在创建
CGGradientRef
,用它来支持alpha通道:

此数组中的项数应为计数和 颜色空间中的组件数。例如,如果颜色 空间是一个RGBA颜色空间,您希望在空间中使用两种颜色 渐变(一个用于起始位置,另一个用于结束位置 位置),然后您需要在组件中提供8个值(红色、绿色、, 蓝色和第一种颜色的alpha值,然后是红色、绿色, 蓝色和第二种颜色的alpha值

以下是完整的视图实现:

h

m

实现AlphaGrad -(id)initWithFrame:(CGRect)帧 { self=[super initWithFrame:frame]; 如果(自我){ } 回归自我; } -(void)drawRect:(CGRect)rect{ CGContextRef ctx=UIGraphicsGetCurrentContext(); CGContextSaveGState(ctx); CGContextClip(ctx); CGGradientRef gradient=[self-NewGradientWithColor:[NSArray Array WithObjects:[UIColor blackColor],[UIColor WithRed:0绿色:0蓝色:0 alpha:0.0f],无] 位置:[NSArray阵列,其对象:@0、@1、nil]]; CGContextDrawLinearGradient(ctx、渐变、CGPointMake(rect.origin.x、rect.origin.y), CGPointMake(rect.origin.x,rect.origin.y+rect.size.height),kCGGradientDrawsAfterEndLocation; CGGradientRelease(梯度); CGContextRestoreGState(ctx); } -(CGGradientRef)使用颜色的新渐变:(NSArray*)颜色阵列位置:(NSArray*)位置阵列{ int count=[colorsArray count]; CGFloat*组件=malloc(sizeof(CGFloat)*4*计数); CGFloat*位置=malloc(sizeof(CGFloat)*计数); 对于(int i=0;i 问题是透明度似乎不受支持,透明部分被淹没为白色:


是否可以使用
CGGradientRef
获得透明度以使“较低”的子视图部分可见?

很抱歉,我没有将
backgroundColor
设置为
clearColor
,从而解决了问题。不过,让我把它留在这里,以备将来参考

@interface AlphaGrad : UIView

@end
@implementation AlphaGrad

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}

-(void) drawRect:(CGRect)rect {

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextSaveGState(ctx);
    CGContextClip(ctx);

    CGGradientRef gradient = [self newGradientWithColors:[NSArray arrayWithObjects:[UIColor blackColor], [UIColor colorWithRed:0 green:0 blue:0 alpha:0.0f], nil]
                                               locations:[NSArray arrayWithObjects:@0, @1, nil]];

    CGContextDrawLinearGradient(ctx, gradient, CGPointMake(rect.origin.x, rect.origin.y),
                                CGPointMake(rect.origin.x, rect.origin.y+rect.size.height), kCGGradientDrawsAfterEndLocation);
    CGGradientRelease(gradient);

    CGContextRestoreGState(ctx);
}

- (CGGradientRef)newGradientWithColors:(NSArray*)colorsArray locations:(NSArray*)locationsArray {

  int count = [colorsArray count];

  CGFloat* components = malloc(sizeof(CGFloat)*4*count);
  CGFloat* locations = malloc(sizeof(CGFloat)*count);

  for (int i = 0; i < count; ++i) {
    UIColor* color = [colorsArray objectAtIndex:i];
    NSNumber* location = (NSNumber*)[locationsArray objectAtIndex:i];
    size_t n = CGColorGetNumberOfComponents(color.CGColor);
    const CGFloat* rgba = CGColorGetComponents(color.CGColor);
    if (n == 2) {
      components[i*4] = rgba[0];
      components[i*4+1] = rgba[0];
      components[i*4+2] = rgba[0];
      components[i*4+3] = rgba[1];
    } else if (n == 4) {
      components[i*4] = rgba[0];
      components[i*4+1] = rgba[1];
      components[i*4+2] = rgba[2];
      components[i*4+3] = rgba[3];
    }
    locations[i] = [location floatValue];
  }

  CGContextRef context = UIGraphicsGetCurrentContext();
  CGColorSpaceRef space = CGBitmapContextGetColorSpace(context);
  CGGradientRef gradient = CGGradientCreateWithColorComponents(space, components, locations, count);
  free(components);
  free(locations);
  return gradient;
}
@end