How to resize an UIImageView (swift)(如何调整 UIImageView 的大小(swift))
问题描述
我是 swift 的初学者,我无法调整 UIImageView 的大小.
I'm a beginner with swift and I can't get the resize of a UIImageView working.
这是我目前的布局:
我想要的是调整图像的大小以占据屏幕宽度的一半(每行 2 张图像).
What I want is to resize the images to occupy half the screen's width (2 images per row).
这是我的故事板:
这是在我的控制器中绘制集合视图的函数:
Here is the function drawing the collection view inside my controller :
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
}
有什么线索可以解决这个问题吗?
Any leads to sort this out ?
推荐答案
你的单元格的大小由你的 UICollectionViewLayout
对象决定,在你的情况下它是一个 UICollectionViewFlowLayout
.如果您希望所有单元格的大小相同,则可以在布局本身上配置属性;itemSize
和 minimumInterimSpacing
可以解决问题.或者,如果您希望您的单元格具有不同的大小,例如根据每个包含的图像或其他内容,您需要您的集合视图委托来实现 UICollectionViewDelegateFlowLayout
方法:
The sizes of your cells are decided by your UICollectionViewLayout
object, which is a UICollectionViewFlowLayout
in your case. If you want all of your cells to be the same size, you can configure the properties on the layout itself; itemSize
and minimumInterimSpacing
will do the trick. Alternatively, if you want your cells to be different sizes, say depending on the image each contains or whatever, you needs your collection view delegate to implement the UICollectionViewDelegateFlowLayout
method:
optional func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize
为每个单元格返回您想要的大小.
returning the size you want for each cell.
这篇关于如何调整 UIImageView 的大小(swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何调整 UIImageView 的大小(swift)
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01