image has to be circular without empty spaces in swift - swift

I have a image downloaded from the web and i want to show the image in circular form.right now the image is not in circular form.It shows white spaces on top and bottom of the imageview.
The code used for the image is as follows:
self.profileview.layer.cornerRadius = self.profileview.frame.height / 2
self.profileview.layer.borderWidth = 1.0
self.profileview.layer.borderColor = UIColor.black.cgColor
self.profileimg.clipsToBounds = true
The image which appears is as follows:
enter image description here
How to make the image circular?

try this , may be it will work in your case .
myimageview.contentMode = .scaleAspectFill
or
myimageviewcontentMode = .scaleToFill

Related

macOS - Programmatically display an image using NSImageView

Using a Swift app targeting the macOS platform, I am trying to programmatically create an NSImageView object, assign an image to it, and then display it, using:
let imageViewObject = NSImageView();
let img = NSImage(cgImage: winCGImage, size: NSZeroSize);
imageViewObject.image = img
self.window.contentView?.addSubview(imageViewObject)
However, no image is displayed when I run this.
On the debugger, I can see that imageViewObject.image (i.e. img) has a valid image, and I can view it using the eye icon.
What am I missing?
You are creating an image view with a zero frame, first create the image and then the view passing the proper size.
let img = NSImage(cgImage: winCGImage, size: NSZeroSize)
let imageViewObject = NSImageView(frame: NSRect(origin: .zero, size: img.size))
imageViewObject.image = img
Avoid to create views with the default initializer ().

How to change image's size? Image does not fill

I'm trying to make the image rounded. It is already 128x128 (width and height) and the corner radius is 64. I want to zoom in before cutting the photo to take the full rounded size of the picture.
How to zoom it?
My code:
profileImage.layer.cornerRadius = 64
profileImage.clipsToBounds = true
I want to make it like this:
You should set the UIImageView's content mode to .scaleAspectFill. That way, the image will fill up the whole container without distorting it.
profileImage.contentMode = .scaleAspectFill
please first ensure your image height and width must be equal. and constraint set properly
after that change below to line
profileImage.layer.cornerRadius = profileImage.frame.size.width/2
profileImage.clipsToBounds = true

iOS 10: Custom UITabBar background image produces border at top of image. How do I remove it?

Anyone have a working solution to get rid of this border in iOS 10? I have a custom UITabBar background image.
I've tried the following with no results:
tabBar.backgroundImage = UIImage(named: "myBackgroundImage.png")
tabBar.shadowImage = nil
tabBar.shadowImage = UIImage()
// i've also tried combinations of this in the storyboard directly
I finally threw my hands up in the air and set the bar style to "Black".. this doesn't get rid of the border, but makes it white. So it hides it.
If you use backgroundImage then shadow line will come
so you can try this :
self.tabBar.backgroundImage = UIImage()
self.tabBar.shadowImage = UIImage()
let tabBarView = UIImageView(image: #imageLiteral(resourceName: "YOUR_IMAGE"))
tabBarView.frame = CGRect(x: 0, y: 49 - IMAGEHEIGHT, width: SCREENWIDTH, height: IMAGEHEIGHT)
self.tabBar.addSubview(tabBarView)
self.tabBar.sendSubview(toBack: tabBarView)
It work for me
This was happening to me because my image was taller than the default tab bar of 49. Making sure my background image height was exactly 49 made this line disappear (96 for 2x and 147 for 3x).
Hope it helps!
Try this:
tabBar.layer.borderWidth = 0
tabBar.layer.borderColor = .clear
Are you sure there is no border in the image itself?

uibezierpath with multiple line width and a background image (swift)

I'm trying to draw several shapes (rectangle, triangle, circle, ...) in swift and for that, I use uibezierpath but I am not able to draw exactly what I want.
I need to draw for example a rectangle, but the borders of this rectangle need to have different line width.
To do that, I create different path then use the "appendpath" to merge them in one path. So that works, BUT I also need to have a background image in this rectangle.
For that, I create a layer and set it an image. The issue is that, no background image are displayed when I use "appendpath", certainly because it doesn't recognize my drawing as a rectangle.
I hope it is clear enough, but is there a way to draw a shape with a background image, and have different border width ?
Thanks for your help !!
There are two solutions I'd suggest you try:
1) Masking
Create a normal CALayer and set the image as its contents. Then create a CAShapeLayer with the path you like and use it as the first layer's mask.
E.g.:
let imageLayer = CALayer()
imageLayer.contents = UIImage(named: "yourImage")?.CGImage // Your image here
imageLayer.frame = ... // Define a frame
let maskPath = UIBezierPath(...) // Create your path here
let maskLayer = CAShapeLayer()
maskLayer.path = maskPath.CGPath
imageLayer.mask = maskLayer
Don't forget to set the right frames and paths, and you should be able to achieve the effect you wanted.
2) Fill color
Create a CAShapeLayer with the path you like, then use your image as its fillColor.
E.g.:
let path = UIBezierPath(...) // Create your path here
let layer = CAShapeLayer()
layer.path = path.CGPath
let image = UIImage(named: "yourImage") // Your image here
layer.fillColor = UIColor(patternImage: image!).CGColor
You may find this approach easier at first, but controlling the way the image fills your shape is not trivial at all.
I hope this will help.
If you'd like more details, please provide an image or a sketch of what you're trying to achieve and / or the code you've written so far. Thanks!

UIImageView with custom border

I've a problem with UIImageView with custom border.
how can i make an image like this?
I add the normal image with bread, and i have this image, the border
any idea?
thanks
All you need to do is to use a bucket fill tool to fill the star outer border.
Afterwards, just create a new UIImageView with the original imageView frame and put it on top of the original image using "bringToFront()" method:
let imgView_bread = UIImageView(image: UIImage (named: "bread"))
imgView_bread.contentMode = .ScaleAspectFill
self.view.addSubview(imgView_bread);
let imgView_starBorder = UIImageView(image: UIImage (named: "star_border"))
imgView_starBorder.contentMode = .ScaleAspectFill
imgView_starBorder.frame = imgView_bread.frame
self.view.addSubview (imgView_starBorder);
self.view.bringSubviewToFront (imgView_starBorder);