数据写入本地


    //数组写入本地
    //1.写的东西
    NSArray* arr = @[@"1",@"2",@"3"];
    //2.写进哪里
    NSString *str2 = [documentStr stringByAppendingPathComponent:@"fuckyou.avi"];
    NSLog(@"%@",documentStr);
    //3、写进去
    [arr writeToFile:str2 atomically:YES];
    //4. 写完拿出来
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    NSLog(@"%@",array);

    
    //字典写入本地
    // 直接写入本地的时候 第一次写入之后。 如果再次对同一个文件进行写入操作,会覆盖之前的。
    //一次只能写入一条数据
    //如果不想覆盖,就先读出来,然后一起在写进去
    //所有能直接写入本地的 一定要遵守NSCoding协议
    //    // 1⃣️ 写的东西
    NSDictionary *dic = @{@"小强":@"傻强"};
    //    // 2⃣️ 写到哪里
    NSString *str3 = [documentStr stringByAppendingString:@"/ss.tet"];
    
    //    // 3⃣️ 写进去
    [dic writeToFile:str3 atomically:YES];
    //    // 4⃣️ 写完拿出来试试
    NSDictionary *dicc = [NSDictionary dictionaryWithContentsOfFile:path];
    NSLog(@"document===== %@",documentStr);
    NSLog(@"dicc === %@",dicc);
    

 


    //图片写入本地
    NSString *str =@"http://img.fs0757.com/news/2015/0901//2015090110400752.jpg";
    NSURL *url = [NSURL URLWithString:str];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSString *pathh = [documentStr stringByAppendingString:@"/猴子.png"];
    [data writeToFile:pathh atomically:YES];
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    NSData *data1 = [NSData dataWithContentsOfFile:pathh];
    UIImage *image = [UIImage imageWithData:data1];
    img.image = image;
    [self.view addSubview:img];
    NSLog(@"path == =%@",path);

    

posted @ 2015-10-06 15:30  Xizi。  阅读(155)  评论(0编辑  收藏  举报