Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do set a width and height of an Image in Swift

Tags:

ios

swift

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

like image 762
user3763693 Avatar asked Jul 12 '14 03:07

user3763693


People also ask

How do I change the width and height of an image in Swift?

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.

How do I change the size of an image in Xcode?

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.

How do I assign an image in Swift?

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.

How do I change the frame size in Swift?

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.


2 Answers

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); 
like image 130
iPatel Avatar answered Sep 19 '22 05:09

iPatel


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 ;-)

**

EDIT

**

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) 
like image 24
YannSteph Avatar answered Sep 19 '22 05:09

YannSteph