【问题标题】:UIImage from a URL that change by date来自按日期更改的 URL 的 UIImage
【发布时间】:2011-12-07 11:21:51
【问题描述】:

我的问题很简单,我想从 URL 加载 UIImage,但这个 URL 会按日期以编程方式更改。

例如今天的网址是http://www.abc.com/2011-10-13/alfa.jpg

明天是http://www.abc.com/2011-10-14/alfa.jpg

唯一改变的是日期部分,我如何计算我的应用程序在每次启动时在当前日期加载“alfa.jpg”?

谢谢!

【问题讨论】:

  • 您可以使用 NSDate 类获取当前日期并将其作为参数传递...

标签: iphone ios uiimage nsurl


【解决方案1】:

您应该使用[NSDate date],它根据设备时间返回当前日期和时间(假设您的设备时间与实际时间相差不超过一天)。然后你应该根据你的网址格式化日期。有点像-

NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString* dateString = [formatter stringFromDate:[NSDate date]];
[formatter release];

【讨论】:

  • 你刚刚离开,mm 将提取分钟而不是月份。
【解决方案2】:

这是一项简单的任务,只需使用当前日期格式化您的 URL 字符串,例如,

NSString *URLString = [[NSString alloc] initWithFormat:@"http://www.abc.com/%@/alfa.jpg", currentDate];

然后使用此字符串进行 URL 请求。

【讨论】:

    【解决方案3】:

    使用NSDateFormatter 生成字符串中随时间变化的部分。

    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSString * datePart = [dateFormatter stringFromDate:[NSDate date]];
    
    NSString * theURLString = [NSString stringWithFormat:@"http://www.abc.com/%@/alfa.jpg", datePart];
    

    您必须维护一种机制来检查当天的图像是否已下载,以避免再次下载。

    【讨论】:

    • 您正在泄漏 dateFormatter。 ;-)
    【解决方案4】:

    创建您正在使用的 url 的模板 nstring 和 DateFormatter 对象...

    NSDateFormatter *df   = [[[NSDateFormatter alloc] init] autorelease];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSString *todayStr    = [df stringFromDate:[NSDate date]];
    
    NSString *todayUrl    = [NSString stringWithFormat:@"http://www.abc.com/%@/alfa.jpg", todayStr];
    

    todayUrl 有今天的网址!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-16
      相关资源
      最近更新 更多