[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:

[IOS]网络操作+图片的下载和读取+json数据读取

Demo源文件下载(猛戳)


当前标题:[IOS]网络操作+图片的下载和读取+json数据读取
当前网址:http://cdiso.cn/article/joeoph.html