[IOS]网络操作+图片的下载和读取+json数据读取-创新互联
如何读取沙盒中的文件,和保存网络资源到沙盒中?
成都网站建设、成都网站设计服务团队是一支充满着热情的团队,执着、敏锐、追求更好,是创新互联的标准与要求,同时竭诚为客户提供服务是我们的理念。成都创新互联把每个网站当做一个产品来开发,精雕细琢,追求一名工匠心中的细致,我们更用心!-(NSString *)dataFilePath:(NSString*)fileName
{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *document=[paths objectAtIndex:0];
return [documentstringByAppendingPathComponent:fileName];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//可以下载图片
[self.datawriteToFile:[selfdataFilePath:@"image.jpg"]atomically:YES];
self.webView.hidden =YES;
//将沙盒中的图片加载到界面中
NSString *path = [selfdataFilePath:@"image.jpg"];
UIImage *imag = [[UIImagealloc]initWithContentsOfFile:path];
UIImageView *img = [[UIImageViewalloc]initWithImage:imag];
CGRect rect = CGRectMake(0,0,320,460);
img.frame = rect;
[self.viewaddSubview:img];
}
进入主题,接下来我要实现三个功能:
1.访问网页
2.从网上加载图片资源到本地
3.发送get请求获取到天气预报的接口,然后保存到本地接着是json解析
功能设计:
1.webView的使用
PageViewController.h:
#import@interface PageViewController : UIViewController @property (retain, nonatomic) IBOutlet UIWebView *webView; - (IBAction)GoClick:(id)sender; @property (retain, nonatomic) IBOutlet UITextField *txtUrl; - (IBAction)resignBoardClick:(id)sender; @property(retain,nonatomic)NSURL *url; @property(nonatomic,retain)UIAlertView *alert; @end
PageViewController.m:
// // PageViewController.m // 地图+网络 // // Created by 丁小未 on 13-8-27. // Copyright (c) 2013年 dingxiaowei. All rights reserved. // #import "PageViewController.h" @interface PageViewController () @end @implementation PageViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = @"页面"; } return self; } -(void)pageLoad1:(NSURL *)url { NSURLRequest *request = [NSURLRequest requestWithURL:url]; [self.webView loadRequest:request]; } - (void)viewDidLoad { [super viewDidLoad]; self.url = [NSURL URLWithString:@"http://www.baidu.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:self.url]; [self pageLoad1:self.url]; } -(void)webViewDidFinishLoad:(UIWebView *)webView { [self.alert dismissWithClickedButtonIndex:0 animated:YES]; } -(void)webViewDidStartLoad:(UIWebView *)webView { self.alert = [[UIAlertView alloc] initWithTitle:@"Loading..." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles: nil]; [self.alert show]; UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; aiv.center = CGPointMake(self.alert.bounds.size.width/2, self.alert.bounds.size.height/2); [aiv startAnimating]; [self.alert addSubview:aiv]; } - (void)dealloc { [_webView release]; [_url release]; [_txtUrl release]; [_alert release]; [super dealloc]; } - (IBAction)GoClick:(id)sender { [self.txtUrl resignFirstResponder]; if (self.txtUrl.text != nil) { self.url = [NSURL URLWithString:self.txtUrl.text]; [self pageLoad1:self.url]; } else { self.alert = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"地址不能为空" delegate:self cancelButtonTitle:@"OK"otherButtonTitles: nil]; [self.alert show]; } } - (IBAction)resignBoardClick:(id)sender { [self.txtUrl resignFirstResponder]; } @end
xib:
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网站名称:[IOS]网络操作+图片的下载和读取+json数据读取-创新互联
网站路径:http://azwzsj.com/article/djciih.html