我是一个初学者,而且我无法调整UIImageView的大小。如何调整UIImageView的大小(swift)
这是我目前的布局:
我想是调整图像以占据屏幕的宽度的一半(每行2个图像)。
这是我的故事板:
下面是函数绘制集合视图我的控制器内:
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {
var cell : MenuInspirationCellView = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as MenuInspirationCellView
cell.imageView.layer.borderWidth = 2
cell.imageView.layer.borderColor = UIColor.yellowColor().CGColor
//This is my first approach trying to modify the frame :
cell.imageView.frame = CGRectMake(0,0, self.view.bounds.width/2,v120)
var cellImage : UIImage = UIImage(data: NSData(contentsOfURL: NSURL(string: images[indexPath.row])))
cell.imageView.image = cellImage;
//This is my second approach (based on http://www.snip2code.com/Snippet/89236/Resize-Image-in-iOS-Swift) :
// to resize an image to dimension 52x52
//var newSize:CGSize = CGSize(width: 52,height: 52)
//let rect = CGRectMake(0,0, newSize.width, newSize.height)
//UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
// image is a variable of type UIImage
//cellImage.drawInRect(rect)
//let newImage = UIGraphicsGetImageFromCurrentImageContext()
//UIGraphicsEndImageContext()
// resized image is stored in constant newImage
//cell.imageView.image = newImage;
//This is my thrid approach (based on https://gist.github.com/hcatlin/180e81cd961573e3c54d, of course i added his functions but I don't show them here for the sake of readability) :
//cell.imageView.image = self.RBSquareImageTo(cellImage, size: CGSize(width: 80, height: 80))
return cell
}
任何线索来排序了这一点?
谢谢!现在我可以动态调整我的单元格大小。但我怎么能调整每个单元格内的图像? – 2014-09-03 14:01:47
没问题,@AlexandreNucera。通过调整xib或代码中的各种自动布局约束,设置'UIImageView'的'autoresizingMask'属性或手动在'MenuInspirationCellView'的'layoutSubviews'函数中执行布局,可以调整每个单元格内的图像的大小。 – stefandouganhyde 2014-09-03 17:41:29
谢谢,这正是我所需要的。我通过修改我的约束得到了这个工作(我删除了固定的宽度和高度)。 – 2014-09-04 09:56:27