iOS网络图片保存到本地

来源:互联网 发布:手机题库制作软件 编辑:程序博客网 时间:2024/06/16 09:40

说明

//共享数据 —-> 特殊的PC (本地服务器,远程服务器)

//C/S

//IP 10.20.157.135 —> 域名

//14.215.177.38 –>

//区分资源 —> URL

//ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=4271053251,2424464488&fm=116&gp=0.jpg

//URL —>
//C/S —> http https –> safe 身份验证 8080

//ftp:// —> 共享

//file 本地文件

//数据上传 —> usrname usrpasswd
//协议名://域名(IP):端口/路径?参数(参数名=参数值)

获取网络图片并保存

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //xocde7 —> http

    //URL
    //127.0.0.1 (本机地址) —>
    NSString *urlStr = @”http://127.0.0.1/net/text“;

    //NSURL —> URL 对象
    NSURL *url = [[NSURL alloc] initWithString:urlStr];

    NSError *err = nil;
    //根据url对象取得资源
    NSString *webString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&err];
    NSLog(@”%@”,webString);

    //访问图片 —> 文件类型 文本文件
    //图片 mp3,mp4 二进制文件
    urlStr = @”http://f.hiphotos.baidu.com/news/q%3D100/sign=3cc948f7642762d0863ea0bf90ed0849/b7003af33a87e950a00f04b817385343faf2b4ce.jpg“;
    NSURL *imageURL = [NSURL URLWithString:urlStr];

    NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
    UIImageView *iamgeView = [[UIImageView alloc] initWithFrame:self.view.bounds];

    //加载图片对应的二进制数据
    iamgeView.image = [UIImage imageWithData:imageData];

    [self.view addSubview:iamgeView];

    //
    //保存图片
    //1.要保存图片
    //2.保存成功回调的对象
    //3.对象的方法
    //4.contextInfo 对象的方法传参数
    UIImageWriteToSavedPhotosAlbum(iamgeView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }

//回调方法
- (void)image:(UIImage )image didFinishSavingWithError:(NSError )error contextInfo:(void *)contextInfo
{
if(error == nil)
{
NSLog(@”保存成功”);
//UIAlertView
}
else
{
NSLog(@”保存失败 —> %@”,error);
}
}

0 0
原创粉丝点击