【问题标题】:download multiple images on iOS app在 iOS 应用上下载多张图片
【发布时间】:2013-08-12 23:34:39
【问题描述】:

我使用此代码将图像从网络下载到我的 iOS 应用程序。

https://github.com/AshFurrow/AFImageDownloader

[AFImageDownloader imageDownloaderWithURLString:@"http://static.ashfurrow.com.s3.amazonaws.com/github/worked.jpg" autoStart:YES completion:^(UIImage *decompressedImage) {
self.imageView.image = decompressedImage;
}];

如您所见,此代码仅下载一张图片...

如何一次下载更多图片?

假设我的图片名称是这样的:

xy.png 其中 x 是从 1 到 999 的数字,y 是从 1 到 4 的数字 例如:1651.png、1652.png、1653.png、1654.png - 如您所见,我的图像名称的最后一位数字是“y”,从 1 到 4...这是规则,我的所有图像名称以 1、2、3、4 结尾。

但是,165 是“x”,所以下一组图像将是 1661.png、1662.png、1663.png、1664.png

我希望你明白我的意思.. 所以,我需要下载名称从 11.png 到 9994.png 的所有图像

有什么想法吗?如何使用上述代码下载并以原名保存。

提前致谢

【问题讨论】:

  • 首先,您可以在后台线程中循环调用相同的函数。其次,我真的希望这些是非常小的图像,即使它们是大量的图像,也会迫使用户下载。
  • 是的。它们很小,一个图像最大 200kb ......好吧,我可以将循环用于“x”作为图像名称的第一部分,那么第二部分“y”呢?你能给我一个带有两个初始化的循环的例子吗?我是新人:)
  • 为什么不能对 y 部分使用 for 循环? 10,000 张图片需要 200kb?那是2GB的下载量!有些数据计划甚至没有那么高。
  • 我将此代码用于类似 4 pics 1 song game 的游戏......所以,用户将下载多达 1000-4000 张图片,不再......
  • 没有。绝对不。不要下载 1000-4000 200kb 的图像。如果我发现一个应用程序占用了这么多空间,我会立即删除它并称其为垃圾应用程序(我会在我的 1 星评论中使用更多丰富多彩的词)。只下载你需要的图片,所以如果它像 4 张图片 1 首歌,下载像 12 张图片这样你就下载了第一轮,然后缓存了接下来的两个,并像 @Jsdodgers 说的那样使用 for 循环

标签: ios objective-c


【解决方案1】:

听说过嵌套的 for 循环吗?

static const NSUInteger xLowerBound = 1;
static const NSUInteger xUpperBound = 999;
static const NSUInteger yLowerBound = 1;
static const NSUInteger yUpperBound = 4;

static NSString *const path = @"http://static.ashfurrow.com.s3.amazonaws.com/";
static NSString *const imgExt = @"jpg";

for (NSUInteger x = xLowerBound; x <= xUpperBound; x++) {
    for (NSUInteger y = yLowerBound; y <= yLowerBound; y++) {
        NSString *combined = [NSString stringWithFormat:@"%lu%lu", (unsigned long)x, (unsigned long)y];
        NSString *urlString = [path stringByAppendingPathComponent:[combined stringByAppendingPathExtension:imgExt]];

        [AFImageDownloader imageDownloaderWithURLString:urlString autoStart:YES completion:^(UIImage *decompressedImage) {
            // Do stuff
        }
    }
}

也就是说,您可能不应该这样做。 =P

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多