【问题标题】:To download images from server and overwrite only modified images从服务器下载图像并仅覆盖修改后的图像
【发布时间】:2013-09-12 11:09:03
【问题描述】:

我是 iPhone 的新手请告诉我如果我想第一次下载 100 张图片,然后第二次在所有 100 张图片中只有 10 张图片会被修改,我想覆盖那 10 张图片怎么做??

【问题讨论】:

  • 图片下载信息(即图片名称和下载链接)来自哪里?是来自网络服务吗?
  • 是的,来自 web 服务并使用该 url 我想在 imageview 上显示图像。
  • 请发布一些格式的回复,以便我们给你一个代码
  • 如果有任何帮助接受答案

标签: iphone ios xml json


【解决方案1】:

第一次同步存储同步时间。并在下一次同步中将这个时间传递给 web 服务。因此,在此响应中,您将仅获得在上次同步时间后更新的那些记录,并且仅更新该图像。但为此,您必须在您的网络服务中添加时间标签。

【讨论】:

    【解决方案2】:

    如果服务器响应给出图像属性,例如 url 除外让我们考虑具有唯一 ID 的图像。然后将 imageurl 和图像 ID 保存到字典中,同时保存所有图像,即在第一次下载期间。

    如果服务器进行了修改,那么在该响应中,您将使用某些属性获得修改后的图像。现在存储修改后的图像 ID 并下载它们。

    您能否按照此处回答的 SO 问题进行操作。

    iOS - Download file only if modified (NSURL & NSData)

    里面有代码

    我最终使用这种方法来检测文件上的修改日期: *发现于HERE

    -(bool)isThumbnailModified:(NSURL *)thumbnailURL forFile:(NSString *)thumbnailFilePath{
        // create a HTTP request to get the file information from the web server
        NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:thumbnailURL];
        [request setHTTPMethod:@"HEAD"];
    
        NSHTTPURLResponse* response;
        [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    
        // get the last modified info from the HTTP header
        NSString* httpLastModified = nil;
        if ([response respondsToSelector:@selector(allHeaderFields)])
        {
            httpLastModified = [[response allHeaderFields]
                                objectForKey:@"Last-Modified"];
        }
    
        // setup a date formatter to query the server file's modified date
        // don't ask me about this part of the code ... it works, that's all I know :)
        NSDateFormatter* df = [[NSDateFormatter alloc] init];
        df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
        df.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
        df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    
        // get the file attributes to retrieve the local file's modified date
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSDictionary* fileAttributes = [fileManager attributesOfItemAtPath:thumbnailFilePath error:nil];
    
        // test if the server file's date is later than the local file's date
        NSDate* serverFileDate = [df dateFromString:httpLastModified];
        NSDate* localFileDate = [fileAttributes fileModificationDate];
    
        NSLog(@"Local File Date: %@ Server File Date: %@",localFileDate,serverFileDate);
        //If file doesn't exist, download it
        if(localFileDate==nil){
            return YES;
        }
        return ([localFileDate laterDate:serverFileDate] == serverFileDate);
    }
    

    【讨论】:

      【解决方案3】:

      希望这会有所帮助。

      首先,您必须跟踪哪些图像被修改,然后只下载那些。

      为此,您有 2 种方法:

      1)您可以在服务器端设置不同的图像名称,当它被修改时,而不是在下载时首先调用一个列出图像名称的网络服务。将这些名称与下载的图像名称(即在您的文档目录中)进行比较。如果它们与下载不同,则不然。

      2) 您可以制作存储先前下载图像信息的本地数据库,并在第二次下载时比较这些值。如果与下载不同,则不然。

      【讨论】:

      • 我不想让你耗时数据库比较数据
      • @user2750118 比你可以使用第一种方式。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      相关资源
      最近更新 更多