以编程方式调整 UIImage 的大小不起作用 [英] resize UIImage programmatically doesn't work
本文介绍了以编程方式调整 UIImage 的大小不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图以编程方式调整我的图像大小以适应屏幕的大小,但是当我构建我的应用程序时,图像甚至不会显示.我只看到一个空白屏幕,有人知道我做错了什么吗?
I was trying to resize my image programmatically to adapt to the screen's size, but when I build my app, the image won't even show. I just see a blank screen, does anyone know what I did wrong?
这是我的代码(从其他一些关于调整图像大小的线程中了解到):
This is my code (learned that from some other threads about resizing images):
class ViewControllerSport: UIViewController {
@IBOutlet weak var FotoSport: UIImageView!
let screen = UIScreen.mainScreen().bounds
override func viewDidLoad() {
super.viewDidLoad()
FotoSport.frame = CGRect(x: 20, y: 20, width: screen.width * 0.5, height: screen.width * 0.5)
FotoSport.image = UIImage(named: "Blokker")
}
推荐答案
以下函数调整图像大小.它需要两个参数:图像和所需的大小.
The following function resizes an image. It takes two arguments: the image and the desired size.
func ResizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
let size = image.size
let widthRatio = targetSize.width / image.size.width
let heightRatio = targetSize.height / image.size.height
// Figure out what our orientation is, and use that to form the rectangle
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSizeMake(size.width * heightRatio, size.height * heightRatio)
} else {
newSize = CGSizeMake(size.width * widthRatio, size.height * widthRatio)
}
// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRectMake(0, 0, newSize.width, newSize.height)
// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.drawInRect(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
用法:
self.ResizeImage(UIImage(named: "MyImage.png")!, targetSize: CGSizeMake(320.0, 700.0))
参考链接:: 调整图片大小
Reference link :: Resize image
Swift 3.0:
func ResizeImage(_ image: UIImage, targetSize: CGSize) -> UIImage? {
let size = image.size
let widthRatio = targetSize.width / image.size.width
let heightRatio = targetSize.height / image.size.height
// Figure out what our orientation is, and use that to form the rectangle
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
} else {
newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
}
// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
用法:
self.ResizeImage(UIImage(named: "MyImage.png")!, targetSize: CGSize(width: 320.0, height: 700.0))
这篇关于以编程方式调整 UIImage 的大小不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文