一般来说,Swift
和iOS
编程的新手。我无法弄清楚如何设置图像的宽度和高度
例如,
let backgroundImage = UIImage(named: "background.png")
由于
答案 0 :(得分:27)
我的建议是。
而不是设置图片的尺寸,您应该设置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);
答案 1 :(得分:15)
您有此代码,此代码设置宽度和高度并保存相同的位置。
/**
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
UIGraphicsBeginImageContextWithOptions(sizeChange, !hasAlpha, scale)
image.drawInRect(CGRect(origin: CGPointZero, size: sizeChange))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
return scaledImage
}
let size = CGSizeMake(100, 150)
myImageView.image! = imageResize(myImageView.image!,sizeChange: size)
在viewDidLoad中工作或刷新“reloadInputViews()”你的UIImageView ;-)
**
**
如果你愿意,你可以创建这个扩展。
创建文件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
}
}
使用(继承UIView)
inheritsOfUIView.setHeight(height: 100)
button.setHeight(height: 100)
view.setHeight(height: 100)
答案 2 :(得分:13)
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!
}
}
答案 3 :(得分:6)
如果你想保持宽高比,这里有一个扩展方法 斯威夫特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
}
}