我想用https://www.pinterest.com 3在collectionView中显示像collectionView这样的图像。为此,图像来自后端,只有URL,而不是图像大小。所以我想要根据图像的高度动态显示细胞。
我要做的是,
let url = self.myArray[indexPath.row].image
let data = NSData(contentsOf:URL(string: url)!)
var photo = UIImage()
if (data?.length)! > 0 {
photo = UIImage(data:data! as Data)!
}
let boundingRect = CGRect(x: 0, y: 0, width: width, height: CGFloat(MAXFLOAT))
let rect = AVMakeRect(aspectRatio: photo.size, insideRect: boundingRect)
return rect.size.height它把我的身高还给我。
问题是:图像是高清的,高达3MB大小。
let data = NSData(contentsOf:URL(string: url)!)花了这么多时间,因为我有20到30张照片。
是否有任何方法可以将图像下载到另一个线程上而不是主线程上,以便在后台继续下载图像,
或者任何人都有一个更好的解决方案来从URL计算图像的高度。
PS:我跟踪了https://www.raywenderlich.com/107439/uicollectionview-custom-layout-tutorial-pinterest for customLayout。
提前谢谢。
发布于 2017-05-18 01:17:45
你应该调查一下阿拉莫火。它使用承诺异步地发出请求,而不阻塞UI线程。您的代码应该如下所示
Alamofire.request(URL_STRING, method: .get, parameters: nil, encoding: URLEncoding.default, headers: getHeaders())
.validate()
.responseImage(completionHandler: {response in
switch response.result {
case .success:
//The image will be stored in response.data in NSData format
break
case .failure(let error):
//Handle errors over here
break
}
})https://stackoverflow.com/questions/44030975
复制相似问题