[英]Changing the size and location of a UIImage within UIImageView (swift)
如果要创建尺寸为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.