我正在尝试向 UImageView 添加阴影
我正在尝试向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您传递的 CGContextRef 也有剪切设置,以基本上防止这种确切的行为。您可能想尝试仅添加 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: