[英]Changing the size and location of a UIImage within UIImageView (swift)
In Swift, Im struggling to programically resize an image ( and its location ) within a UIImageView as follows: 在Swift中,Im努力以编程方式在UIImageView中调整图像(及其位置)的大小,如下所示:
var housePic = UIImage(named:"house")
var houseImageView = UIImageView(image: housePic)
I want to be able create a 2nd imageView with a cropped / resized version of the above houseImageView using the same image. 我希望能够使用相同的图像使用上述houseImageView的裁剪/调整大小版本创建第二个imageView。 The result is to show a section of the image from a grid like this..
结果是显示像这样的网格中图像的一部分。
All my efforts of resizing gives the following wrong result. 我所有的调整大小的努力都得出以下错误结果。 I think i need to somehow resize the image and change the imageView bounds ??
我想我需要以某种方式调整图像大小并更改imageView范围? If required, I can post lots of example failed code.
如果需要,我可以发布很多示例失败的代码。
If you want to create a 2nd image with size 100,100 and add this image to the previous UIImageView
you can do this: 如果要创建尺寸为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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.