我试图添加一个阴影到UImageView [英] I am trying to add a drop shadow to a UImageView

查看:130
本文介绍了我试图添加一个阴影到UImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图添加一个阴影到UIImage视图。我得到一个阴影,但它被剪辑到图像视图的边缘,我不知道为什么,因为我正确设置uiimageview.clipsToBounds为NO。下面是代码:

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


推荐答案

重新传递也有裁剪集,以防止基本上这种确切的行为。您可以尝试只添加CALayer:

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

这篇关于我试图添加一个阴影到UImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆