我正在尝试向UIimageView添加投影

时间:2009-12-21 22:36:56

标签: iphone cocoa-touch uikit dropshadow

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

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

1 个答案:

答案 0 :(得分:2)

我相信你传递的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];