Shadow + cornerRadius + scaleAspectFit in imageView?

Welcome
Guys, who faced, it is necessary to implement UIImageView - round corners and shadow

1. You may wish to implement in the mode of the UIImageView.contentMode = .scaleAspectFit

2. UIImageView , you can add as subview in UIView, but then how to pass view to imageView frame? If you putview.frame = imageView.frame, it does not work

3. Anyone has solution for all these three aspects?
April 7th 20 at 15:23
1 answer
April 7th 20 at 15:25
To the frame of the UIImageView coincide with the image size, you need to get the dimensions of the UIImage. In practice, have aspect ratio of UIImage, and resinite frame with this ratio.
Now the picture will always be inscribed in the frame.
Think about it logically, to fit the frame under the picture, not the picture under the frame if you use .scaleAspectFit.

Rounds the corners of the
Now that the frame has the same dimensions as the image, rounding the corners will work. The problem is solved.

Shadows
Depends on the method. If you activate the mask, the shadows will not be visible. Make a container in which is placed a UIImageView, for it put the mask. And the shadow put the container.
Poluchaetsya following

imageView.contentMode = .scaleAspectFit
 imageView.layer.cornerRadius = 25
 imageView.clipsToBounds = true

 shadowView.clipsToBounds = false
 shadowView.layer.cornerRadius = 25
 shadowView.layer.shadowRadius = 6
 shadowView.layer.shadowColor = UIColor.darkGray.cgColor
 shadowView.layer.shadowOffset = CGSize(width: 3, height: 1)
 shadowView.layer.shadowOpacity = 0.7

 let wi = image.size.width / 4
 let hi = image.size.height / 4

 self.imageView.frame.size.width = wi
 self.imageView.frame.size.height = hi

 self.shadowView.frame.size.width = wi
 self.shadowView.frame.size.height = hi


In this case, running shadow and fillets
But if you add constrains, then everything stops working.

imageView.translatesAutoresizingMaskIntoConstraints = false
 imageView.leftAnchor.constraint(with equalto: view.leftAnchor, constant: 18).isActive = true
 imageView.rightAnchor.constraint(with equalto: view.rightAnchor, constant: -18).isActive = true
- Sister.Brakus commented on April 7th 20 at 15:28
First you kept the proportions, and then a constraint-AMI got rid of them. Of course will not work. - aurelie_Crooks commented on April 7th 20 at 15:31
@Yadira_Beer69, Welcome

1. I can tell that you want to correct?
func imageViewSize(image: UIImage) {

 let wi = image.size.width / 3.14
 let hi = image.size.height / 3.14

 shadowView.frame.size.width = wi
 shadowView.frame.size.height = hi

 shadowView.clipsToBounds = false
 shadowView.layer.cornerRadius = 25
 shadowView.layer.shadowRadius = 9
 shadowView.layer.shadowColor = UIColor.darkGray.cgColor
 shadowView.layer.shadowOffset = CGSize(width: 3, height: 3)
 shadowView.layer.shadowOpacity = 0.8

 imageView.frame = shadowView.bounds
 imageView.contentMode = .scaleAspectFit
 imageView.layer.masksToBounds = true
 imageView.layer.cornerRadius = 25
 imageView.image = image

createConstrainsShadowView()
 }


2. How can you get rid of such a decision?
let wi = image.size.width / 3.14
 let hi = image.size.height / 3.14

That would not reduce the image size so that it adjusted to the size of the ShadowView

3. How to center shadowView X-KSU
shadowView.translatesAutoresizingMaskIntoConstraints = false

 NSLayoutConstraint(item: shadowView,
 attribute: .top
 relatedBy: .greaterThanOrEqual,
 toItem: view,
 attribute: .topMargin,
 multiplier: 1,
 constant: 64).isActive = true

 NSLayoutConstraint(item: shadowView,
 attribute: .bottom
 relatedBy: .lessThanOrEqual,
 toItem: svButton,
 attribute: .bottomMargin,
 multiplier: 1,
 constant: -70).isActive = true

 NSLayoutConstraint(item: shadowView,
 attribute: .centerY,
 relatedBy: .equal
 toItem: view,
 attribute: .centerY,
 multiplier: 1,
 constant: -10).isActive = true
- Sister.Brakus commented on April 7th 20 at 15:34

Find more questions by tags Swift