rounded imageView 's border is not smooth after setting image property - ios

class PPAvatarCollectionCell: UICollectionViewCell {
var imageView:UIImageView!
override init(frame: CGRect) {
super.init(frame: frame)
imageView = UIImageView(frame: CGRect(origin: CGPointMake(0, 0), size: CGSizeMake(frame.size.width, frame.size.height)))
self.addSubview(imageView)
imageView.contentMode = .ScaleAspectFill
imageView.backgroundColor = UIColor.whiteColor()
imageView.layer.borderColor = UIColor.greenColor().CGColor
imageView.layer.borderWidth = 10
imageView.image = UIImage(named: "demo")
imageView.layer.cornerRadius = frame.size.width*0.5
imageView.clipsToBounds = true
}
the border is smooth and great with above code
but after add imageView.image = UIImage(named: "demo")
the imageView's border is no longer smooth.
Why did this happen ?
UPDATE:
Seems something wrong with layer.radius ,the border is smooth even with image property set on imageView after remove imageView.layer.cornerRadius = frame.size.width*0.5
UPDATE 2:
turn out to be something wrong with UICollectionViewCell , imageView is a part of UICollectionViewCell

u can use 2 imageview 1 to show image and second to hide and show circular image.
1st imageview show image as it is.
2nd imageview which will have white background and green circle above 1st image and middle of image will be transparent

Basically, your image should be a square. Than do these:
imageView.layer.cornerRadius = self.frame.height / 2
imageView.layer.masksToBounds = false
imageView.clipsToBounds = true
imageView.image = "yourSquareImage"
Check the full solution with easy using extension:
https://stackoverflow.com/a/36102901/2125010

Related

UIImageview not getting rounded

I am trying to create a circular image and I have implemeted the right code but I do not know why it does not get rounded below is my code
lazy var profileImage: UIImageView = {
let image = UIImageView()
image.translatesAutoresizingMaskIntoConstraints = false
image.layer.borderColor = UIColor.blue.cgColor
image.layer.borderWidth = 1.5
image.image = UIImage(named: "prof.jpg")
image.contentMode = .scaleAspectFit
image.layer.cornerRadius = image.frame.size.width / 2
image.clipsToBounds = true
return image
}()
and my constraints are
fileprivate func layout() {
view.addSubview(profileImage)
NSLayoutConstraint.activate([
profileImage.centerXAnchor.constraint(equalTo: view.centerXAnchor),
profileImage.centerYAnchor.constraint(equalTo: view.centerYAnchor),
profileImage.heightAnchor.constraint(equalToConstant: 200),
profileImage.widthAnchor.constraint(equalToConstant: 200),
])
}
layout() is then added to viewDidLoad
You’re not giving your UIImageView a frame when it’s initialised so it uses .zero as a default. This means that when you access image.frame.size.width you are getting a value of 0.
What I would suggest is to move your image.layer.cornerRadius = image.frame.size.width / 2 into the viewDidLayoutSubviews override on your UIViewController class.
You could also create a custom class that subclasses UIImageView and implements the same logic. The override for UIImageView would be layoutSubviews.
Image doesn't have a frame *image.layer.cornerRadius = image.frame.size.width / 2* inside the closure.
So set the corner radius after the profileImage is set with its constraints
profileImage.layoutIfNeeded()
profileImage.layer.cornerRadius = profileImage.frame.size.width / 2

How to fill background image in subview in swift 4?

I have used scrollview. Over the Scrollview I have a UIView where I can set up my image as a background. But problem is that image height is not merged with UIView height. It is shown when scrolling bottom. I want add image with whole UIView.
Here is my image screen shot
Here is my code
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.clipsToBounds = true
backgroundImage.image = UIImage(named: "background_image")
backgroundImage.contentMode = .scaleToFill
self.my_view_2.insertSubview(backgroundImage, at:0)
Please Help me
Thanks
Height of your UIImageView is same as screen size, if your view is greater than screen size, then white view will remain at bottom.
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
Update frame of backgroundImage with scroll's view, as:
let backgroundImage = UIImageView(frame: self.my_view_2.bounds)
backgroundImage.autoresizingMask = [.flexibleHeight, .flexibleWidth] // In case if your my_view_2 frames increases
self.my_view_2.insertSubview(backgroundImage, at: 0)
Add this code in your viewDidLoad(). May be helps you to solve.
if #available(iOS 11.0, *) {
scrollView.contentInsetAdjustmentBehavior = .never
} else {
automaticallyAdjustsScrollViewInsets = false
}
try this
let backgroundImage = UIImageView(frame: my_view_2.bounds)
backgroundImage.clipsToBounds = true
backgroundImage.image = UIImage(named: "background_image")
backgroundImage.contentMode = .scaleToFill
self.my_view_2.addSubview(backgroundImage)
Hope it may help
self.my_view_2.layer.contents = #imageLiteral(resourceName: "background_image");

tintcolored area to have border in uiimageview

let templateImage = UIImage(named:"marker")?.withRenderingMode(.alwaysTemplate)
self.imageView.image = templateImage
self.imageView.contentMode = .scaleAspectFit
self.imageView?.tintColor = UIColor.red
self.imageView?.tintAdjustmentMode = .normal
self.imageView.backgroundColor = .clear
I'm using Swift 3
But what I require is the tintcolored area border to be filled with black color as below screenshot:
Marker icon is here:
Are you trying to border the content of the image. If yes, then it is not possoble.
You can give border to imageview using below lines:
self.imageView.layer.borderwidth = 1 //as per your requirement
self.imageView.layer.borderColor = UIColor. blackColor().CGColor

iOS - Set ImageView background color and mask

How can I set the background color of an ImageView for an icon and have it mask to bounds? So that the inner transparent parts of the image are then the background color?
Example: in this twitter icon, how can I set the inner white (transparent) parts of the icon to be a different color?
EDIT: What if the image is NOT a circle? But any shape.
You have to follow the following steps
imageView.image = UIImage(named: "yourImageName")?.withRenderingMode(.alwaysOriginal)
imageView.layer.cornerRadius = 14 //number of your choice
imageView.layer.masksToBounds = true
imageView.tintColor = .black
imageView.backgroundColor = .blue //Sets the background color
imageView.layer.cornerRadius = imageView.width/2 //Crops the image to be a circle
imageView.layer.masksToBounds = true //Sets the mask to bounds
If you want a custom shape you can do this:
create two imageViews (above each other)
first imageView use as usual
second imageView use as background shape
Code for the one in foreground
firstImageView.backgroundColor = .clear
Second imageView (in the background)
secondImageView.image = UIImage(named: "yourImage").withRenderingMode(.alwaysTemplate)
secondImageView.tintColor = yourBackgroundColor
secondImageView.backgroundColor = .clear

How to change resize imageview in titleview - SWIFT 3

I would like to load an image to the titleview and then change the size.
I'm using this code :
let logo = UIImage(named: "namelogo")
let imageView = UIImageView(frame: CGRect(x:0, y:0, width:70, height:30))
imageView.image = logo
self.navigationItem.titleView = imageView
But this is not working properly. I can play with the height, but the width is not changing !!!

Resources