更改 UIImageView 中 UIImage 的大小和位置 (swift)
Changing the size and location of a UIImage within UIImageView (swift)
在 Swift 中,我正在努力以编程方式调整 UIImageView 中图像(及其位置)的大小,如下所示:
var housePic = UIImage(named:"house")
var houseImageView = UIImageView(image: housePic)
我希望能够使用同一图像创建第二个 imageView,其中包含上述 houseImageView 的裁剪/调整大小版本。结果是从这样的网格中显示图像的一部分..
我调整大小的所有努力都给出了以下错误结果。我想我需要以某种方式调整图像大小并更改 imageView 边界??
如果需要,我可以 post 大量示例失败代码。
如果您想创建尺寸为 100,100 的第二张图片并将此图片添加到之前的图片中 UIImageView
,您可以这样做:
let imageName = "house.png"
let originalImage = UIImage(named:imageName)!
let imageView = UIImageView(image: originalImage)
// this is only to visualization purpose
imageView.backgroundColor = UIColor.lightGrayColor()
self.view.addSubview(imageView)
// create a new image resizing it
let destinationSize = CGSizeMake(100, 100)
UIGraphicsBeginImageContext(destinationSize);
originalImage.drawInRect(CGRectMake(0,0,destinationSize.width,destinationSize.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
// add the new image to the UIImageView
imageView.image = newImage
imageView.contentMode = UIViewContentMode.Center
在 Swift 中,我正在努力以编程方式调整 UIImageView 中图像(及其位置)的大小,如下所示:
var housePic = UIImage(named:"house")
var houseImageView = UIImageView(image: housePic)
我希望能够使用同一图像创建第二个 imageView,其中包含上述 houseImageView 的裁剪/调整大小版本。结果是从这样的网格中显示图像的一部分..
我调整大小的所有努力都给出了以下错误结果。我想我需要以某种方式调整图像大小并更改 imageView 边界?? 如果需要,我可以 post 大量示例失败代码。
如果您想创建尺寸为 100,100 的第二张图片并将此图片添加到之前的图片中 UIImageView
,您可以这样做:
let imageName = "house.png"
let originalImage = UIImage(named:imageName)!
let imageView = UIImageView(image: originalImage)
// this is only to visualization purpose
imageView.backgroundColor = UIColor.lightGrayColor()
self.view.addSubview(imageView)
// create a new image resizing it
let destinationSize = CGSizeMake(100, 100)
UIGraphicsBeginImageContext(destinationSize);
originalImage.drawInRect(CGRectMake(0,0,destinationSize.width,destinationSize.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
// add the new image to the UIImageView
imageView.image = newImage
imageView.contentMode = UIViewContentMode.Center