gpt4 book ai didi

ios - 如何在 UIImageView 之上叠加一个部分透明的 UIImageView?

转载 作者:行者123 更新时间:2023-11-28 22:29:55 30 4
gpt4 key购买 nike

我有一个带有固定图像的 UIImageView。我想在顶部添加第二个 UIImageView,我打算旋转它。我需要在运行时创建顶部图像。我找不到在哪里处理透明度。它在 UIImage 中还是在 UIImageView 中?到目前为止,我在顶部的 UIImageView 上有一个黑色背景,但我想看穿这些黑色像素。这是代码:(这是基于 Erica Sadun 的 Core iOS 6 Developers Cookbook )。我想在轮子上添加点以添加一些对控件旋转的反馈。

    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];

最佳答案

您必须使用 NO 指定不透明在 UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, 0.0);否则生成的图像中将没有 alpha channel => 它将具有黑色像素而不是透明像素。

还有:

  • 您不需要在绘制之前清除上下文,因为它已经被清除了。

  • 您不需要压入和弹出上下文,因为您不需要嵌套不同的上下文。

关于ios - 如何在 UIImageView 之上叠加一个部分透明的 UIImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17719230/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com