【问题标题】:How do I work in the frame of the UIImageView when using an asynchronous loading library like SDWebImage or UIImageView+AFNetworking?使用 SDWebImage 或 UIImageView+AFNetworking 等异步加载库时,如何在 UIImageView 的框架中工作?
【发布时间】:2014-02-18 04:19:52
【问题描述】:

SDWebImageAFNetworking 等库添加了基于异步加载的 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


    【解决方案1】:

    Relayout scrollView 所有子视图会花费很多。您想要的效果必须取决于您在下载之前可以从中获取图片大小的服务器中的数据。样式很多,主要有两种方法:

    1. 处理Url:http://xxx.xx.xx.pic_200*150,200*150为图片大小
    2. 服务器返回添加大小数据。 {@"width":200, @"height":150}

    使用大小,可以用默认图片布局scrollView,然后异步加载,下载完成后更新图片。

    希望能帮到你!

    【讨论】:

    • 很遗憾,我无法控制服务器后端。
    猜你喜欢
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多