生成并运行工程,无论什么时候,只要有网络请求,都可以在状态栏中看到一个小的网络风火轮:
现在,即使你的程序在等待一个很慢的web service,用户都知道程序还在运行着! 下载图片
如果你在table view cell上点击,程序会切换到天气的详细画面,并且以动画的方式显示出相应的天气情况。
这非常不错,但目前动画只有一个背景图片。除了通过网络来更新背景图片,还有更好的方法吗!
下面是本文关于介绍AFNetworking的最后内容了:AFImageRequestOperation. 跟AFJSONRequestOperation一样, AFImageRequestOperation封装了HTTP请求:获取图片。
在WeatherAnimationViewController.m 中有两个方法需要实现. 找到updateBackgroundImage: 方法,并用下面的代码替换:
sender { request requestWithURLURLWithString; AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:nil successrequest, self.backgroundImageView.image = image; ; } failurerequest, NSLog,error); }]; [operation start]; }
这个方法初始化并下载一个新的背景图片。在结束时,它将返回请求到的完整图片。
在WeatherAnimationViewController.m中, 你将看到两个辅助方法:imageWithFilename: 和 saveImage:withFilename:, 通过这两个辅助方法,可以对下载下来的图片进行存储和加载。updateBackgroundImage: 将通过辅助方法把下载的图片存储到磁盘中。
接下来找到deleteBackgroundImage: 方法,并用下面的代码替换:
sender path; ; path paths objectAtIndexstringByAppendingPathComponent; *error; defaultManager] removeItemAtPath:path error:&error]; self.weatherDictionary weatherDescription]; [self start:desc]; }
这个方法将删除已经下载的图片,这样在测试程序的时候,你可以再次下载图片。
最后一次:生成并运行工程,下载天气数据,并点击某个cell,以打开详细天气画面。在详细天气画面中,点击Update Background 按钮. 如果你点击的是晴天cell,将会看到如下画面:
你可以在这里下载到完整的工程:.
你所想到的所有方法,都可以使用AFNetworking来与外界通讯:
AFNetworking可以帮助你进行网络开发!