[英]How do I work in the frame of the UIImageView when using an asynchronous loading library like SDWebImage or UIImageView+AFNetworking?
像SDWebImage和AFNetworking這樣的庫增加了基於URL異步設置UIImageView
上的image
功能,因此用戶不必等待圖像加載。
就我的使用而言,我正在創建一個類似於Photos.app的圖庫,但是圖像是從URL加載的。 萬一畫廊說了50張高分辨率圖像,我不希望用戶在加載每個圖像之前就必須等待每個圖像,因此這些庫非常適合我的用例(我將加載第一張圖片,然后按需加載)。
但是,我對如何在尚不知道圖像框架的情況下在視圖中顯示圖像感到困惑,因為尚未下載圖像。 如果每個人都有相同的幀(例如,如果是化身),這將不是問題,但事實並非如此。
就我而言,我使用UIImageViews如下:
// Create image view
self.imageView = [[UIImageView alloc] initWithImage:image];
self.imageView.userInteractionEnabled = YES;
// Create scroll view
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
self.scrollView.delegate = self;
self.scrollView.contentSize = self.imageView.bounds.size;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
// Figure out the zoom extremities, as well as the initial zoom level so full image is visible
CGFloat scrollViewWidthRatio = CGRectGetWidth(self.scrollView.bounds) / self.scrollView.contentSize.width;
CGFloat scrollViewHeightRatio = CGRectGetHeight(self.scrollView.bounds) / self.scrollView.contentSize.height;
CGFloat minScale = MIN(scrollViewWidthRatio, scrollViewHeightRatio);
self.scrollView.minimumZoomScale = minScale;
self.scrollView.zoomScale = minScale;
self.scrollView.maximumZoomScale = 1.0;
// Add image view and scroll view
[self.scrollView addSubview:self.imageView];
[self.view addSubview:self.scrollView];
但是,當圖像異步加載時,這一切似乎都中斷了。 我將幀設置為什么,因為圖像可能是一百萬個不同的大小? 由於相同的問題,我應該將scrollView.contentSize
設置scrollView.contentSize
? 如何計算scrollView的縮放比例?
基本上,當我尚不了解框架或尺寸時,該如何使用呢? 加載圖像后,是否可以刷新所有內容?
Relayout scrollView的所有子視圖將花費很多。 您要獲得的效果必須取決於從Server獲得的數據,然后才能下載圖片。 樣式很多,主要有兩種方法:
使用該大小,您可以使用默認圖像來布局scrollView,然后異步加載,並在下載完成時更新圖像。
希望可以幫到您!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.