【问题标题】:update iOS app with images from web使用来自网络的图像更新 iOS 应用程序
【发布时间】:2014-03-31 17:43:04
【问题描述】:

我正在开发免费应用程序,并有 2 张图片(一张透明覆盖)作为背景。我将每月从我的网络上更改这两张图片。图片的更新过程,但是应用程序如何知道图片是否在网络上发生了变化?启动应用程序时,我想实现以下目标:

  1. 在启动应用程序之前检查可用于图片的更新
  2. 下载图片,存储在本地,用作背景
  3. 如果没有可用的新图像,则使用默认值
  4. 启动应用程序

有人可以指出比较/网络请求过程的正确方向吗?

这是我目前所拥有的:

viewDidLoad 方法

 self.imageData = [[NSMutableData alloc]init];
[NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.blahblah.com/newImage.jpg"]]delegate:self];

其余代码:

  - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 {
    self.length = [response expectedContentLength];
 }
 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.imageData appendData:data];
    float progress =  (float)[imageData length]/(float)self.length;
    [self.progressView setProgress:progress animated:YES];
}
 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    UIImage *img = [UIImage imageWithData:self.imageData];
    self.backgroundImageView.image = img;
    [self saveLocally:self.imageData];
}
// save
- (void)saveLocally:(NSData *)imgData
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSDate *aDate = [NSDate date];
    NSTimeInterval interval = [aDate timeIntervalSince1970];
    NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.jpeg",(int)interval]];
    [imgData writeToFile:localFilePath atomically:YES];
}

感谢您的宝贵时间!

【问题讨论】:

    标签: ios image web uiimageview


    【解决方案1】:

    如果我正确理解您的问题,您可以在这里尝试一些方法。

    一种方法是在网络上发布一些文本文件,其中包含图像文件的文件名和/或大小,如果文件大小与您在本地保存的图像及其大小不匹配,您可以然后更新(即下载新副本)您设备上的图像。

    这样,在下载更大的图像文件之前,您只需下载一个非常小的文件(包含这些数字的文本文件)。

    【讨论】:

    • 谢谢迈克尔,我确实上传了 .txt 文件并仅在内容更改时才下载图片
    【解决方案2】:

    我更喜欢在你的应用程序中使用一些 json 类型的功能,并通过 imagename 键值检查并将旧图像名称字符串与检索 json 数据键值进行比较,如果你发现更改,我最近正在做类似的更改应用程序的类型不完全相同,但是是服务器端应用程序,并且必须提供应用程序的离线模式,所以我通过 json 获得的任何东西我通常都会保存到我的库中,即使图像也是如此,并且下次我检索我的 json 时我只同步新的类别、图像或其他的数据。它的直播视频应用类似于 bbc 的新应用类型

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      • 2016-06-02
      相关资源
      最近更新 更多