New to Swift
and iOS
programming in general. I couldn't figure out how to set a width and height of an image.
For example,
let backgroundImage = UIImage(named: "background.png")
Thanks
Show activity on this post. extension UIImage { func imageResize (sizeChange:CGSize)-> UIImage{ let hasAlpha = true let scale: CGFloat = 0.0 // Use scale factor of main screen UIGraphicsBeginImageContextWithOptions(sizeChange, ! hasAlpha, scale) self. draw(in: CGRect(origin: CGPoint.
To get the native size of the image just select the image and press Command + = on the keyboard. the to re-size it proportionally select the corner and hold down the shift key when you re-size it.
Drag and drop image onto Xcode's assets catalog. Or, click on a plus button at the very bottom of the Assets navigator view and then select “New Image Set”. After that, drag and drop an image into the newly create Image Set, placing it at appropriate 1x, 2x or 3x slot.
1) Control-drag from a frame view (e.g. questionFrame) to main View, in the pop-up select "Equal heights". 2)Then go to size inspector of the frame, click edit "Equal height to Superview" constraint, set the multiplier to 0.7 and hit return.
My suggestion is.
Instead of set size of image you should set size of UIImageView
and then put this image on it so, size of image will be display as per your requirement.
Such like,
var imageView = UIImageView(frame: CGRectMake(100, 150, 150, 150)); // set as you want var image = UIImage(named: "myImage.png"); imageView.image = image; self.view.addSubview(imageView);
You have this code, this code set width and height and save the same position.
/** Resize UIImageView :param: UImage :param: new size CGSize :return: new UImage rezised */ func imageResize (#image:UIImage, sizeChange:CGSize)-> UIImage{ let hasAlpha = true let scale: CGFloat = 0.0 // Use scale factor of main screen // Create a Drawing Environment (which will render to a bitmap image, later) UIGraphicsBeginImageContextWithOptions(sizeChange, !hasAlpha, scale) image.drawInRect(CGRect(origin: CGPointZero, size: sizeChange)) let scaledImage = UIGraphicsGetImageFromCurrentImageContext() // Clean up the Drawing Environment (created above) UIGraphicsEndImageContext() return scaledImage } let size = CGSizeMake(100, 150) myImageView.image! = imageResize(myImageView.image!,sizeChange: size)
Work in viewDidLoad or refresh "reloadInputViews()" your UIImageView ;-)
**
**
Hi create this extends if you want.
Create File Extends.Swift and add this code (add import foundation where you want change height)
/** Extension UIView */ extension UIView { /** Set x Position :param: x CGFloat */ func setX(#x:CGFloat) { var frame:CGRect = self.frame frame.origin.x = x self.frame = frame } /** Set y Position :param: y CGFloat */ func setY(#y:CGFloat) { var frame:CGRect = self.frame frame.origin.y = y self.frame = frame } /** Set Width :param: width CGFloat */ func setWidth(#width:CGFloat) { var frame:CGRect = self.frame frame.size.width = width self.frame = frame } /** Set Height :param: height CGFloat */ func setHeight(#height:CGFloat) { var frame:CGRect = self.frame frame.size.height = height self.frame = frame } }
For Use (inherits Of UIView)
inheritsOfUIView.setHeight(height: 100) button.setHeight(height: 100) view.setHeight(height: 100)
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