Can we set below shape on image with UIImageView
.
Any idea!
Thanks
Like others suggest, I would use the image's mask
property.
Info from Apple about the mask
property:
The layer’s alpha channel determines how much of the layer’s content and background shows through. Fully or partially opaque pixels allow the underlying content to show through but fully transparent pixels block that content. The default value of this property is nil nil.
When configuring a mask, remember to set the size and position of the mask layer to ensure it is aligned properly with the layer it masks.
That last sentence is important!
I used Sketch to create a black image with a transparent background (PNG).
Add it to your Assets.xcassets folder. My image is called profileMask
.
Create an outlet for the image view. I called mine profileImageView
.
override func viewDidLoad() {
super.viewDidLoad()
let maskImageView = UIImageView()
maskImageView.contentMode = .scaleAspectFit
maskImageView.image = #imageLiteral(resourceName: "profileMask")
maskImageView.frame = profileImageView.bounds
profileImageView.mask = maskImageView
}
Note:
Here is what it looks like when run:
Guys, this was a pain to do. I tried to create a Bezier Path but to be honest my skills on making an exact shape to match the mask was impossible for me. A Bezier Path would probably look really good if you have the skills to do that but I don't. :(
So I had the idea, "What if I just use the mask image as the border?" It worked! The benefit of doing it this way is you can use ANY mask shape in the future and it will create a border for it without having to figure out the bezier path.
Here's how I did it:
profileImageView
to the mask and change the tint color to whatever I want the border to be.profileImageView
minus 2 points (to show the underlying mask image which will be the border).profileImageView
.Here's the code in viewDidLoad now:
override func viewDidLoad() {
super.viewDidLoad()
// Add border first (mask image)
profileImageView.contentMode = .scaleAspectFit
profileImageView.tintColor = .lightGray // Border Color
profileImageView.image = #imageLiteral(resourceName: "profileMask")
// Profile Image
let profileImage = UIImageView()
profileImage.image = #imageLiteral(resourceName: "Profile")
profileImage.contentMode = .scaleAspectFill
// Make a little bit smaller to show "border" image behind it
profileImage.frame = profileImageView.bounds.insetBy(dx: 2, dy: 2)
// Mask Image
let maskImageView = UIImageView()
maskImageView.image = #imageLiteral(resourceName: "profileMask")
maskImageView.contentMode = .scaleAspectFit
maskImageView.frame = profileImage.bounds
// Apply mask to profile iamge
profileImage.mask = maskImageView
// Add profile image as a subview on top of the "border" image
profileImageView.addSubview(profileImage)
}
So now it looks like this when we run it:
What could be really cool is if we create a custom UIImageView with a mask and border width property. Then you can just assign these properties and the custom class will take care of the rest. Let me know if you guys want to see something like this and I'll create it and post it here. Maybe also make a YouTube video tutorial for my channel.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With