如何在圆形UIImageView或UIView中添加阴影?

时间:2022-04-13 07:40:36

I am trying to make a circle UIImageView, and it works. Below is the way I use to make it:

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

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

I would like to add some shadow to the UIImageView. The below code does add some shadow to my image view, however, the image view changes back to square shape. Can someone give me some pointers to solve this problem? Below is the code I use to add the shadow:

我想为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;

5 个解决方案

#1


40  

Use the CALayer's shadowPath property and add a UIBezierPath with rounded rect

使用CALayer的shadowPath属性并添加带有圆角rect的UIBezierPath

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

EDIT

For a square-ish image view this technique does not work directly because, as you said, the image view goes back to square. Reason: You set clipsToBounds = NO to show the shadow which removes the clipping for corner radius, where imageView is subview of container.

对于正方形图像视图,此技术不能直接起作用,因为正如您所说,图像视图会回到正方形。原因:您设置clipsToBounds = NO以显示阴影,该阴影将移除角半径的剪裁,其中imageView是容器的子视图。

Workaround:
Add your imageview in a container view and then apply the layer shadow to this container. Following is the code I tried.

解决方法:在容器视图中添加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;

The resultant effect is as shown in the screenshot,

结果效果如屏幕截图所示,

如何在圆形UIImageView或UIView中添加阴影?

Hope that helps!

希望有所帮助!

#2


2  

Without a container but with a background view here is my 2 cents

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

As a swift 2.2 extension

作为一个快速的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)
    }
}

#3


2  

In case anyone looks for Swift 3 or 4 working solution:

如果有人寻找Swift 3或4工作解决方案:

    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)

Sometimes you also need to set constraints for image inside of the container, but it may work without it too in some cases. But if it's not, add this:

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

    // 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

#4


1  

extension UIImageView {
    func addShadow() {
        self.layer.shadowColor = UIColor.black.cgColor
        self.layer.shadowOffset = CGSize(width: 2, height: 5)
        self.layer.shadowOpacity = 0.5
        self.layer.shadowRadius = 1.0
        self.clipsToBounds = false
    }
}
try this code. hope it will help you....

#5


-4  

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;

try this code...

试试这个代码......

#1


40  

Use the CALayer's shadowPath property and add a UIBezierPath with rounded rect

使用CALayer的shadowPath属性并添加带有圆角rect的UIBezierPath

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

EDIT

For a square-ish image view this technique does not work directly because, as you said, the image view goes back to square. Reason: You set clipsToBounds = NO to show the shadow which removes the clipping for corner radius, where imageView is subview of container.

对于正方形图像视图,此技术不能直接起作用,因为正如您所说,图像视图会回到正方形。原因:您设置clipsToBounds = NO以显示阴影,该阴影将移除角半径的剪裁,其中imageView是容器的子视图。

Workaround:
Add your imageview in a container view and then apply the layer shadow to this container. Following is the code I tried.

解决方法:在容器视图中添加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;

The resultant effect is as shown in the screenshot,

结果效果如屏幕截图所示,

如何在圆形UIImageView或UIView中添加阴影?

Hope that helps!

希望有所帮助!

#2


2  

Without a container but with a background view here is my 2 cents

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

As a swift 2.2 extension

作为一个快速的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)
    }
}

#3


2  

In case anyone looks for Swift 3 or 4 working solution:

如果有人寻找Swift 3或4工作解决方案:

    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)

Sometimes you also need to set constraints for image inside of the container, but it may work without it too in some cases. But if it's not, add this:

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

    // 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

#4


1  

extension UIImageView {
    func addShadow() {
        self.layer.shadowColor = UIColor.black.cgColor
        self.layer.shadowOffset = CGSize(width: 2, height: 5)
        self.layer.shadowOpacity = 0.5
        self.layer.shadowRadius = 1.0
        self.clipsToBounds = false
    }
}
try this code. hope it will help you....

#5


-4  

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;

try this code...

试试这个代码......