2014-02-27 125 views
0

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

  • 下载图片之前

    1. 支票可用于图片的更新,本地存储,作为背景使用
    2. 如果没有新的可用的图像,使用默认
    3. 启动应用程序

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

    这是我到目前为止有:

    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]; 
    } 
    

    谢谢您的时间!

  • 回答

    1

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

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

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

    +0

    谢谢迈克尔,我没有上传.txt文件,只有当内容改变时下载图像 – Alessign

    1

    我会更喜欢在你的应用中使用一些json类型的功能,并通过imagename键值检查并比较旧的图像名称字符串和检索json数据键值,如果你发现变化,进行了更改,我最近在做相似类型的应用程序不完全相同,但是是服务器端应用程序,并且必须提供应用程序的离线模式,所以我通过json得到的内容通常会保存到我的图书馆,即使图像以及下一次检索我时json数据我只同步哪些是新的类别,图像或什么。它的直播流视频应用程序种类bbc新的应用程序类型