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

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

Related

image has to be circular without empty spaces in 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

How do you get the aspect fit size of a uiimage in a uimageview?

When print(uiimage.size) is called it only gives the width and the height of the original image before it was scaled up or down. Is there anyway to get the dimensions of the aspect fitted image?
Actually, there is a function in AVFoundation that can calculate this for you:
import AVFoundation
let fitRect = AVMakeRect(aspectRatio: image.size, insideRect: imageView.bounds)
now fitRect.size is the size inside the imageView bounds by maintaining the original aspect ratio.
You're going to need to calculate the resulting image size in Points yourself*.
* It turns out you don't. See Alladinian's answer. I'm going to
leave this answer here to explain what the library function is doing.
Here's the math:
let imageAspectRatio = image.size.width / image.size.height
let viewAspectRatio = imageView.frame.width / imageView.frame.height
var fitWidth: CGFloat // scaled width in points
var fitHeight: CGFloat // scaled height in points
var offsetX: CGFloat // horizontal gap between image and frame
var offsetY: CGFloat // vertical gap between image and frame
if imageAspectRatio <= viewAspectRatio {
// Image is narrower than view so with aspectFit, it will touch
// the top and bottom of the view, but not the sides
fitHeight = imageView.frame.height
fitWidth = fitHeight * imageAspectRatio
offsetY = 0
offsetX = (imageView.frame.width - fitWidth) / 2
} else {
// Image is wider than view so with aspectFit, it will touch
// the sides of the view but not the top and bottom
fitWidth = imageView.frame.width
fitHeight = fitWidth / imageAspectRatio
offsetX = 0
offsetY = (imageView.frame.height - fitHeight) / 2
}
Explanation:
It helps to draw the pictures. Draw a rectangle that represents the
imageView. Then draw a rectangle that is narrow but extends from the
top of the image view to the bottom. That is the first case. Then draw
one where the image is short but extends to the two side of the image
view. That is the second case. At that point, we know one of the
dimensions. The other is just that value multiplied or divided by the
image's aspect ratio because we know that the .aspectFit keeps the
image's original aspect ratio.
A note about frame vs. bounds. The frame is in the coordinate system of the view's superview. The bounds are in the coordinate system of the view itself. I chose to use the frame in this example, because the OP was interested in how far to move the imageView in it's superview's coordinates. For a standard imageView that has not been rotated or scaled further, the width and height of the frame will match the width and height of the bounds. Things get interesting though when a rotation is applied to an imageView. The frame expands to show the whole imageView, but the bounds remain the same.

How to make button width based on text length?

I want to find a way, how to calculate button width based on text size? As I am using localize for translations, so based on different languages, button text changes, and some of them does not fit into the button size.
If you work with autolayout
it's quite easy. You just need to make sure that the constraints that specify the x coordinate (like leading space resp. trailing space, also width) have a lower priority than the value for the horizontal content compression resistance priority.
If you do manually
You can calculate the size of the text with:
guard let buttonFont = button.titleLabel?.font else { return }
let buttonText = "click me"
let size = t.size(withAttributes:[.font: buttonFont])
and then set the button's frame size:
button.frame.size = size

Swift - Rotate an image, then zoom in and crop to keep width/height aspect ratio intact

If the title isn't clear, open Photos app on your iPhone and edit a picture. Try the rotation tool. The frame don't rotate, only the image does and zoom in to stay in the frame.
Here is a picture of this:
I would like to copy this comportment in my Swift project. It means rotating an image (it can be UIImage, CIImage or CGImage) but crop it keeping the same width/height ratio.
Where should I begin ? Thanks for your time.
There's an app a filter for that. CIStraightenFilter rotates an image by any amount while stretching it and cropping it to maintain the original dimensions:
let image: CIImage = /* my image 480x320 */
let rotated = image.applyingFilter("CIStraightenFilter",
withInputParameters: [ kCIInputAngleKey: Double.pi / 6 ]) // 30 degrees
rotated.extent // still 480x320
Here's some examples (rotations of zero, 15°, 30°, and 45°):

How to screenshot just part of a screen

I need to crop an image to a rectangle with a height of 25 and a width the length of the screen.
I have the image ready and positioned in the center of the screen. I'd like to screenshot ONLY the rectangle.
I followed this answer, but it does not seem to be working for me for some reason.
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.view.frame.width,25), false, 0)
self.view.drawViewHierarchyInRect(CGRectMake(self.view.frame.width,25,view.bounds.size.width,view.bounds.size.height), afterScreenUpdates: true)
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
statusImage = image
You are using wrong coordinates. With your code, the drawing starts at the top right corner of the view and draws void space at the right side of it. You should use 0 as X value and a negative Y value. For example, if you want to screenshot a bar starting at Y=50, you should use:
self.view.drawViewHierarchyInRect(CGRectMake(0, -50, view.bounds.size.width, view.bounds.size.height), afterScreenUpdates: true)
If you want to draw from the top of the view, just put 0 as Y value as well.