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:
我想在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;
}
1 个解决方案
#1
2
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:
我相信你传递的cgcontext tref也有剪切集,以防止这种行为。你可以试着添加一个日历:
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];
#1
2
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:
我相信你传递的cgcontext tref也有剪切集,以防止这种行为。你可以试着添加一个日历:
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];