【发布时间】:2014-03-31 17:43:04
【问题描述】:
我正在开发免费应用程序,并有 2 张图片(一张透明覆盖)作为背景。我将每月从我的网络上更改这两张图片。图片的更新过程,但是应用程序如何知道图片是否在网络上发生了变化?启动应用程序时,我想实现以下目标:
- 在启动应用程序之前检查可用于图片的更新
- 下载图片,存储在本地,用作背景
- 如果没有可用的新图像,则使用默认值
- 启动应用程序
有人可以指出比较/网络请求过程的正确方向吗?
这是我目前所拥有的:
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