[英]Swift: How to get async data before method returns?
I'm trying work use the following plugin for a grid layout. 我正在尝试使用以下插件进行网格布局。 https://github.com/ecerney/CollectionViewWaterfallLayout
https://github.com/ecerney/CollectionViewWaterfallLayout
It requires me to return a cgsize for each cell with the actual image size. 它要求我为每个单元格返回一个cgsize的实际图像大小。
So I'm trying to get to the size with another cocoapod plugin: 所以我试图通过另一个cocoapod插件来达到这个目的:
https://github.com/IMcD23/IMRemoteImageSize/ https://github.com/IMcD23/IMRemoteImageSize/
but, "getImageSize" is an async call to get the image I believe. 但是,“ getImageSize”是一个异步调用,用于获取我相信的图像。 Therefore each time return value for cgsize is "nil".
因此,每次cgsize的返回值都是“ nil”。 Is there a way for me to get the size before the return function executes?
我可以在返回函数执行之前获取大小吗? Thanks!
谢谢!
func collectionView(collectionView: UICollectionView, layout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let image = images[indexPath.row]
var sizeOfTopCover:CGSize?
UIImage.getImageSize(image.url, completion: {
(size) -> Void in
print("Got size of image: \(image.url): \(size)")
sizeOfTopCover = size
})
return sizeOfTopCover! <<< breaking here
}
You must be know size when call 通话时必须知道尺寸
func collectionView(collectionView: UICollectionView, layout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
But you can reload your collection view when received sizes for all cells. 但是,当收到所有单元格的大小时,您可以重新加载集合视图。 Or reload each cell when you received size for cell.
或在收到单元格的大小时重新加载每个单元格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.