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

后端 未结 4 1188
慢半拍i
慢半拍i 2020-12-08 04:46

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,

  l         


        
相关标签:
4条回答
  • 2020-12-08 05:18

    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)
    
    0 讨论(0)
  • 2020-12-08 05:21

    if you want to keep the aspect ratio, here is an extension method for Swift 3 :

    import UIKit
    
    extension UIImage {
    
        func resize(maxWidthHeight : Double)-> UIImage? {
    
            let actualHeight = Double(size.height)
            let actualWidth = Double(size.width)
            var maxWidth = 0.0
            var maxHeight = 0.0
    
            if actualWidth > actualHeight {
                maxWidth = maxWidthHeight
                let per = (100.0 * maxWidthHeight / actualWidth)
                maxHeight = (actualHeight * per) / 100.0
            }else{
                maxHeight = maxWidthHeight
                let per = (100.0 * maxWidthHeight / actualHeight)
                maxWidth = (actualWidth * per) / 100.0
            }
    
            let hasAlpha = true
            let scale: CGFloat = 0.0
    
            UIGraphicsBeginImageContextWithOptions(CGSize(width: maxWidth, height: maxHeight), !hasAlpha, scale)
            self.draw(in: CGRect(origin: .zero, size: CGSize(width: maxWidth, height: maxHeight)))
    
            let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
            return scaledImage
        }
    
    }
    
    0 讨论(0)
  • 2020-12-08 05:27

    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);
    
    0 讨论(0)
  • 2020-12-08 05:28

    Swift 3.0 version (from @YannickSteph)

    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.zero, size: sizeChange))
    
            let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
            return scaledImage!
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题