Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Swift中设置图像的宽度和高度_Ios_Swift - Fatal编程技术网

Ios 如何在Swift中设置图像的宽度和高度

Ios 如何在Swift中设置图像的宽度和高度,ios,swift,Ios,Swift,一般来说,Swift和iOS编程的新手。我不知道如何设置图像的宽度和高度。 比如说, let backgroundImage = UIImage(named: "background.png") 谢谢,我的建议很好 您应该设置UIImageView的大小,然后将此图像放在其上,而不是设置图像的大小,这样,图像的大小将根据您的要求显示 诸如此类 var imageView = UIImageView(frame: CGRectMake(100, 150, 150, 150)); // set

一般来说,
Swift
iOS
编程的新手。我不知道如何设置图像的宽度和高度。
比如说,

  let backgroundImage = UIImage(named: "background.png")
谢谢,我的建议很好

您应该设置
UIImageView
的大小,然后将此图像放在其上,而不是设置图像的大小,这样,图像的大小将根据您的要求显示

诸如此类

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

你有这个代码,这个代码设置宽度和高度并保存相同的位置

/**
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)
在视图中工作加载或刷新UIImageView的“reloadInputViews()”;-)

**

编辑 **

嗨,如果需要,创建此扩展

创建文件扩展.Swift添加此代码(在需要更改高度的位置添加导入基础)

供使用(继承UIView)


Swift 3.0版本(来自@YannickSteph)


如果您想保持纵横比,这里有一个扩展方法 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
    }

}

为什么不改为设置
UIImageView
的帧大小?除非您确实希望使用上下文绘制全新的
UIImage
。如果您的imageView已经声明并且具有宽度和高度限制,该怎么办?如何更改它?imageView.frame=CGRect(x:100,y:150,width:150,height:150)这对我不起作用,因为它与imageView对宽度和高度的自动布局约束冲突。要使建议在Swift 3或更高版本中起作用,请将第一行替换为:var imageView=UIImageView(frame:CGRect)(x:0,y:0,width:50,height:50))将高度和宽度放入
CGSize
对象中,并在调用该对象时将其传递给
imageResize
方法。
inheritsOfUIView.setHeight(height: 100)
button.setHeight(height: 100)
view.setHeight(height: 100)
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!
    }

}
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
    }

}