2013-07-18 120 views
0

我有一个UIImageView与固定的图像。我想添加第二个UIImageView,我打算旋转。我需要在运行时创建顶部图像。我无法找到处理透明度的地方。它是在UIImage还是在UIImageView中?到目前为止,我在顶端的UIImageView上有一个黑色的背景,但我希望看到这些黑色像素。这是代码:(这是根据埃里卡Sadun的Core iOS 6 Developers Cookbook)。我想在轮子上添加点来为控制旋转添加一些反馈。如何在UIImageView之上分层部分透明的UIImageView?

float width=200, height=200; 

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), YES, 0.0); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    UIGraphicsPushContext(context); 
    CGContextAddRect(context, CGRectMake(0, 0, width, height)); 
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]); 
    CGContextFillPath(context); 

    float lineWidth=4; 
    CGContextAddEllipseInRect(context, CGRectInset(CGRectMake(0, 0, width, height), lineWidth, lineWidth)); 
    CGContextSetLineWidth(context, 4); 
    CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]); 
    CGContextStrokePath(context); 
    UIGraphicsPopContext(); 

    dots = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    UIImageView * ivDots = [[UIImageView alloc] initWithImage:dots]; 
    UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wheel.png"]]; 

    [self addSubview:iv]; 
    [self addSubview:ivDots]; 
+0

在顶部图像视图中使用的图像是透明的吗?即它在某个地方是否具有透明度? – Fogmeister

+0

我在运行时创建它,我期待它具有透明背景,因为我使用[UIColor clearColor]填充UIImage的相同大小的矩形。请参阅代码中的第7行。 – alecail

回答

2

必须指定不透明与UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, 0.0);NO否则就会在所得图像中没有alpha通道=>它将有黑色像素,而不是透明的。

另外:

  • 你不需要绘制之前,疏通脉络,因为它已经被清除。

  • 您不需要推送和弹出上下文,因为您没有嵌套不同的上下文。

相关问题