ios 如何在 Swift 中设置图像的宽度和高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24709244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do set a width and height of an Image in Swift
提问by user3763693
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,
新手Swift
和iOS
编程一般。我不知道如何设置图像的宽度和高度。
例如,
let backgroundImage = UIImage(named: "background.png")
Thanks
谢谢
回答by iPatel
My suggestion is.
我的建议是。
Instead of set size of imageyou should set size of UIImageView
and then put this imageon it so, size of imagewill be display as per your requirement.
而不是设置图像的大小,您应该设置大小,UIImageView
然后将此图像放在上面,这样,图像的大小将根据您的要求显示。
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);
回答by odemolliens
Swift 3.0version (from @YannickSteph)
Swift 3.0版本(来自@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!
}
}
回答by YannSteph
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 ;-)
在 viewDidLoad 中工作或刷新“reloadInputViews()”你的 UIImageView ;-)
**
**
EDIT
编辑
**
**
Hi create this extends if you want.
嗨,如果你愿意,可以创建这个扩展。
Create File Extends.Swiftand addthis code (add import foundationwhere you want change height)
创建文件 Extends.Swift并添加此代码(在要更改高度的位置添加导入基础)
/**
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)
供使用(UIView 的继承)
inheritsOfUIView.setHeight(height: 100)
button.setHeight(height: 100)
view.setHeight(height: 100)
回答by Ashkan Ghodrat
if you want to keep the aspect ratio, here is an extension method for Swift 3 :
如果你想保持纵横比,这里是 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
}
}