我怎样才能将阴影添加到一个圆圈UIImageView或UIView?

我正在试图做一个圆UIImageView ,它的工作原理。 以下是我使用它的方式:

 [self.pic.layer setMasksToBounds:YES]; [self.pic.layer setCornerRadius:50.0]; 

我想给UIImageView添加一些阴影。 下面的代码确实为我的图像视图添加了一些阴影,但是,图像视图变回正方形。 有人可以给我一些指针来解决这个问题吗? 以下是我用来添加阴影的代码:

 self.pic.layer.shadowColor = [UIColor purpleColor].CGColor; self.pic.layer.shadowOffset = CGSizeMake(0, 1); self.pic.layer.shadowOpacity = 1; self.pic.layer.shadowRadius = 1.0; self.pic.clipsToBounds = NO; 

使用CALayershadowPath属性,并添加一个四舍五入的rect的UIBezierPath

 self.pic.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.pic.frame cornerRadius:50.0].CGPath; 

编辑

对于正方形的图像视图,这种技术不能直接工作,因为如你所说,图像视图回到正方形。 原因:您设置clipsToBounds = NO以显示删除angular落半径裁剪的阴影,其中imageViewcontainer子视图。

解决方法:
在容器视图中添加您的imageview,然后将图层阴影应用到此容器。 以下是我试过的代码。

 [self.imageView.layer setCornerRadius:60.0]; [self.imageView.layer setMasksToBounds:YES]; self.imageView.clipsToBounds = YES; self.container.backgroundColor = [UIColor clearColor]; self.container.layer.shadowColor = [UIColor blackColor].CGColor; self.container.layer.shadowOffset = CGSizeMake(5,15); self.container.layer.shadowOpacity = 0.5; self.container.layer.shadowRadius = 2.0; self.container.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.container.bounds cornerRadius:100.0].CGPath; 

由此产生的效果如截图所示,

在这里输入图像说明

希望有所帮助!

没有容器,但有一个背景视图,这里是我的2美分

作为一个迅速的2.2扩展

  image?.applyCircleShadow(5, shadowOpacity: 1) 
 extension UIView { func applyCircleShadow(shadowRadius: CGFloat = 2, shadowOpacity: Float = 0.3, shadowColor: CGColor = UIColor.blackColor().CGColor, shadowOffset: CGSize = CGSize.zero) { layer.cornerRadius = frame.size.height / 2 layer.masksToBounds = false layer.shadowColor = shadowColor layer.shadowOffset = shadowOffset layer.shadowRadius = shadowRadius layer.shadowOpacity = shadowOpacity } } extension UIImageView { override func applyCircleShadow(shadowRadius: CGFloat = 2, shadowOpacity: Float = 0.3, shadowColor: CGColor = UIColor.blackColor().CGColor, shadowOffset: CGSize = CGSize.zero) { // Use UIImageView.hashvalue as background view tag (should be unique) let background: UIView = superview?.viewWithTag(hashValue) ?? UIView() background.frame = frame background.backgroundColor = backgroundColor background.tag = hashValue background.applyCircleShadow(shadowRadius, shadowOpacity: shadowOpacity, shadowColor: shadowColor, shadowOffset: shadowOffset) layer.cornerRadius = background.layer.cornerRadius layer.masksToBounds = true superview?.insertSubview(background, belowSubview: self) } } 

如果有人在寻找Swift 3或4工作解决scheme:

  let imageSize: CGFloat = 64.0 // Create a container which has a shadow let imageCotainer = UIView(frame: CGRect(x: 0, y: 0, width: imageSize, height: imageSize)) imageCotainer.clipsToBounds = false imageCotainer.layer.shadowColor = UIColor.black.cgColor imageCotainer.layer.shadowOpacity = 0.2 imageCotainer.layer.shadowOffset = CGSize(width: 0, height: 1) imageCotainer.layer.shadowRadius = 2 // Create an image view that will be inserted into the container view let imageView = UIImageView(frame: imageCotainer.bounds) imageView.image = yourImage imageView.clipsToBounds = true let cornerRadius = imageView.frame.height / 2 imageView.layer.cornerRadius = cornerRadius // Draw a shadow imageCotainer.layer.shadowPath = UIBezierPath(roundedRect: imageCotainer.bounds, cornerRadius: cornerRadius).cgPath // Add image into container imageCotainer.addSubview(imageView) 

有时你也需要为容器内的图像设置约束条件,但在某些情况下也可能没有。 但是,如果不是,请添加以下内容:

  // Set constraints for the image inside the container view imageView.translatesAutoresizingMaskIntoConstraints = false imageView.topAnchor.constraint(equalTo: imageCotainer.topAnchor).isActive = true imageView.leftAnchor.constraint(equalTo: imageCotainer.leftAnchor).isActive = true imageView.rightAnchor.constraint(equalTo: imageCotainer.rightAnchor).isActive = true imageView.bottomAnchor.constraint(equalTo: imageCotainer.bottomAnchor).isActive = true imageView.heightAnchor.constraint(equalToConstant: imageSize).isActive = true imageView.widthAnchor.constraint(equalToConstant: imageSize).isActive = true 
 yourImageView.layer.masksToBounds = NO; yourImageView.layer.shadowOffset = CGSizeMake(5, 0); yourImageView.layer.shadowRadius = 5; yourImageView.layer.shadowOpacity = 0.5; yourImageView.layer.shadowPath = [UIBezierPath bezierPathWithRect:yourImageView.bounds].CGPath; 

试试这个代码…