[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://bzwzjz.com/article/joeoph.html
Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都品牌网站建设 成都h5网站建设 成都网站制作 成都网站制作 H5网站制作 成都网站建设 成都网站设计 成都网站设计 成都商城网站建设 高端定制网站设计 古蔺网站建设 成都网站制作 重庆企业网站建设 成都网站制作公司 成都网站建设 成都网站设计 外贸网站设计方案 成都企业网站制作 梓潼网站设计 品牌网站建设 企业网站建设 专业网站设计