【发布时间】:2014-02-18 04:19:52
【问题描述】:
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的缩放比例?
基本上,如果我还不知道框架或尺寸的任何概念,我该如何使用它?有没有办法在加载图像时刷新所有这些?
【问题讨论】:
标签: ios objective-c uiimageview afnetworking sdwebimage