Iphone 设置CGContext透明背景

Iphone 设置CGContext透明背景,iphone,graphics,background,transparent,cgcontext,Iphone,Graphics,Background,Transparent,Cgcontext,我仍在努力与上下文划清界限。我实际上已经去画直线了,但是现在我需要矩形的背景是透明的,这样现有的背景就会显示出来。以下是我的测试代码: (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor); CGContextSetAlp

我仍在努力与上下文划清界限。我实际上已经去画直线了,但是现在我需要矩形的背景是透明的,这样现有的背景就会显示出来。以下是我的测试代码:

(void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextSetAlpha(context,0.0);
    CGContextFillRect(context, rect);

    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(context, 5.0);
    CGContextMoveToPoint(context, 100.0,0.0);
    CGContextAddLineToPoint(context,100.0, 100.0);
    CGContextStrokePath(context);
}

有什么想法吗?

UIGraphicsGetCurrentContext()
调用
CGContextClearRect(context,rect)

编辑: 好的,明白了

带有该行的自定义视图应具有以下内容:

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

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, rect);
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(context, 5.0);
    CGContextMoveToPoint(context, 100.0,0.0);
    CGContextAddLineToPoint(context,100.0, 100.0);
    CGContextStrokePath(context);
}
我的测试将其用作非常基本的UIViewController:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *v = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [v setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:v];
    TopView *t = [[TopView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:t];
    [v release];
    [t release];
}

理解这里的问题有困难,但如果您无法通过正在绘制的“顶”视图显示“背景”UIView,一种解决方案是
topView.backgroundColor=[UIColor clearColor]我(我想)遇到了同样的问题,这为我解决了这个问题

CGContextClearRect(context,rect) 
如果提供的上下文是窗口或位图上下文,Quartz将有效地清除矩形。对于其他上下文类型,Quartz以设备相关的方式填充矩形。但是,您不应在窗口或位图上下文以外的上下文中使用此函数。

简易方法:

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

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, rect);
    //your code
}

我有同样的问题,然后我发现它是。 我覆盖的init方法是
-(id)initWithFrame:(CGRect)rect.
在这个方法中,
self.background=[UIColor clearColor]
但是我在xib文件中使用这个视图!!!将调用init方法的是

-(id)initWithCoder:(NSCoder*)aDecoder;

因此,覆盖所有的init方法和setup BackgroundColor将正常工作。

您可以使用以下代码创建图像上下文:

cacheContext = CGBitmapContextCreate (cacheBitmap, size.width, size.height, 8, bitmapBytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast);
CGContextSetRGBFillColor(cacheContext, 0, 0, 0, 0);
CGContextFillRect(cacheContext, (CGRect){CGPointZero, size});

关键是KCGimageAlphaPremultipledLast。

这是我使用InterfaceBuilder手动添加的UIImage的工作原理

- (id)initWithCoder:(NSCoder *)aDecoder {

    if(self = [super initWithCoder:aDecoder]) {
        self.backgroundColor = [UIColor clearColor];
    }

    return self;
}


-(void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();    
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(context, 5.0);
    CGContextMoveToPoint(context, 100.0,0.0);
    CGContextAddLineToPoint(context,100.0, 100.0);
    CGContextStrokePath(context);
}

David Kanarek的答案仅在手动创建自己的UIImageView时有效。如果您已经创建了UIView并通过Interface Builder手动添加了它,那么您将需要一种类似这样的方法来调用initWithCoder方法。

Init a context with
opaque==false
,Swift 3

UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale) 
不透明

指示位图是否不透明的布尔标志。如果知道位图完全不透明,请指定true以忽略alpha通道并优化位图的存储。指定false意味着位图必须包含alpha通道才能处理任何部分透明的像素


背景为黑色,必须是iphone背景。我实际上是想在UIView上画画。我不太明白。是否确实已将UIView添加到superview?此drawRect位于您要在其上绘制的UIView中,还是位于其他UIView中?您是否意外设置了此UIView的背景色?谢谢您的提问。我希望IM将UIView添加到它的superview中。绘制矩形肯定在视图中,因为线绘制正确。是的,我已经在UIView中设置了一个背景,我想通过它显示背景。我所要做的就是在现有视图上画一条线。在西雅图受挫。Clear Rect在这里做不到。上面的这些对我都不起作用,即使它是在xib中设置的,只是在代码中设置它就行了+1我将其用于表格视图单元格中的附件指示器。self.opaque=无效,而接受答案中的[self-setBackgroundColor:[UIColor clearColor]]无效。self.opaque=false在创建UICollectionViewCell的自定义子视图时帮助了我。这应该是一个接受的答案。self.opaque=NO使用drawRect使视图透明。非常感谢。这也是我的问题。屏幕上的图层是透明的,尽管
isOpaque
为真。因此,我删除了作为第二个参数的
self.isOpaque
,并想知道为什么生成的
UIImage
不是。不要忘记关闭上下文以避免内存泄漏:
UIGraphicsEndImageContext()