我正在尝试向 UImageView 添加阴影

发布于 2024-08-15 14:27:02 字数 1135 浏览 5 评论 0原文

我正在尝试向 UIImage 视图添加阴影。我得到了一个阴影,但它被剪裁到图像视图的边缘,我不确定为什么,因为我正确地将 uiimageview.clipsToBounds 设置为“否”。下面是代码:

-(void)addShadow
{
   UIGraphicsBeginImageContext(self.frame.size);
   CGContextRef myContext =  UIGraphicsGetCurrentContext();
   float           myColorValues[] = {0, 0, 0, darkness};// 3
   CGColorRef      myColor;// 4
   CGColorSpaceRef myColorSpace;
   CGContextSaveGState(myContext);// 6

   myColorSpace = CGColorSpaceCreateDeviceRGB ();// 9
   myColor = CGColorCreate (myColorSpace, myColorValues);// 10
   CGContextSetShadowWithColor (myContext, myShadowOffset, spread, myColor);// 11
   // Your drawing code here// 12
   // CGContextDrawImage(myContext, rotatingView.frame,imgRef);

   rotatingView.clipsToBounds = NO;
   [rotatingView.image drawInRect:rotatingView.frame
                        blendMode:kCGBlendModeNormal alpha:.5];
   CGColorRelease (myColor);// 13
   CGColorSpaceRelease (myColorSpace); // 14

   UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
   CGContextRestoreGState(myContext);
   UIGraphicsEndImageContext();
   rotatingView.image = imageCopy;
}

I am trying to add a drop shadow to a UIImage view. I get a shadow but it is clipped to the edges of the image view and I am not sure why since I correctly set the uiimageview.clipsToBounds to NO. Below is the code:

-(void)addShadow
{
   UIGraphicsBeginImageContext(self.frame.size);
   CGContextRef myContext =  UIGraphicsGetCurrentContext();
   float           myColorValues[] = {0, 0, 0, darkness};// 3
   CGColorRef      myColor;// 4
   CGColorSpaceRef myColorSpace;
   CGContextSaveGState(myContext);// 6

   myColorSpace = CGColorSpaceCreateDeviceRGB ();// 9
   myColor = CGColorCreate (myColorSpace, myColorValues);// 10
   CGContextSetShadowWithColor (myContext, myShadowOffset, spread, myColor);// 11
   // Your drawing code here// 12
   // CGContextDrawImage(myContext, rotatingView.frame,imgRef);

   rotatingView.clipsToBounds = NO;
   [rotatingView.image drawInRect:rotatingView.frame
                        blendMode:kCGBlendModeNormal alpha:.5];
   CGColorRelease (myColor);// 13
   CGColorSpaceRelease (myColorSpace); // 14

   UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
   CGContextRestoreGState(myContext);
   UIGraphicsEndImageContext();
   rotatingView.image = imageCopy;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澜川若宁 2024-08-22 14:27:02

我相信您传递的 CGContextRef 也有剪切设置,以基本上防止这种确切的行为。您可能想尝试仅添加 CALayer:

CALayer                         *layer = [CALayer layer];
CGRect                          bounds = self.bounds;

layer.bounds = bounds;
layer.position = CGPointMake(bounds.size.width / 2 + 5, bounds.size.height / 2 + 5);
layer.backgroundColor = [UIColor colorWithWhite: 0.10 alpha: 0.75].CGColor;
layer.zPosition = -5;

[self.layer addSublayer: layer];

I believe the CGContextRef you're passed also has clipping set, to prevent basically this exact behavior. You might want to try just adding a CALayer:

CALayer                         *layer = [CALayer layer];
CGRect                          bounds = self.bounds;

layer.bounds = bounds;
layer.position = CGPointMake(bounds.size.width / 2 + 5, bounds.size.height / 2 + 5);
layer.backgroundColor = [UIColor colorWithWhite: 0.10 alpha: 0.75].CGColor;
layer.zPosition = -5;

[self.layer addSublayer: layer];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文