美文网首页
数据写入到文件夹中

数据写入到文件夹中

作者: 娘亲Joanna | 来源:发表于2017-12-01 17:34 被阅读68次

    文件路径的含义

    • var/mobile/Containers/Data/Application/0F51C27A-1282-4F11-8AB3-D8ACCC1A4A58/Documents/uzfs/A123456789/shareImage2/shareImage.png

    • /var/mobile/Containers/Data/Application/应用名称/Documents/文件夹/shareImage.png(图片)

    • base64转图片,把图片写入路径中

      NSData *decodedImageData = [[NSData alloc]
                               initWithBase64EncodedString:@"iVBORw0KGgoAAAANSUhEUgAAAGYAAABmCAYAAAA53+RiAAAABGdBTUEAALGPC/xhBQAAATZJREFUeAHt3MENgkAABMA7KrAEO1E6tzL1CwkvXruX8aUYk2WWz8bAGF4ECBAgQIAAAQIECBAgQIDAugJz3VO7PrPHc3+N7+99/W3I0W1+tpAoYhAgQIAAAQIECBAgQIBAuYDln1ig5Z/YikwECBAgQIAAAQIECBDoFLD8E3uz/BNbkYkAAQIECBAgQIAAAQKdApZ/Ym+Wf2IrMhEgQIAAAQIECBAgQKBTYFbc995pez+15X/fzi8JECBAgAABAgQIECBA4CzgP/+zR8Ynyz+jBykIECBAgAABAgQIECCwgoDln9ii5Z/YikwECBAgQIAAAQIECBDoFLD8E3uz/BNbkYkAAQIECBAgQIAAAQKdApZ/Ym+Wf2IrMhEgQIAAAQIECBAgQKBTIGb5e9Lg4QKy/A8YYW//k6Ak/6eMQP8AAAAASUVORK5CYII=" 
                               options:NSDataBase64DecodingIgnoreUnknownCharacters];
        
        UIImage *decodedImage = [UIImage imageWithData:decodedImageData];
    
        if (decodedImage == nil) {
            NSLog(@"base64转图片失败");
            return; 
        }else{
           //base64图片转成功,保存到某个路径下
               //1.看路径是否存在,不存在就创建
               BOOL isDir = NO;
               NSFileManager *fileManager = [NSFileManager defaultManager];
               //文件路径是否存在, 文件路径是否是路径 
               BOOL existed = [fileManager fileExistsAtPath:@"已知的文件夹路径字符串" isDirectory:&isDir];
        if (!(isDir == YES && existed == YES) )
        {
         BOOL    creatSuccess = [fileManager createDirectoryAtPath:createDir withIntermediateDirectories:YES attributes:nil error:nil];
            //标记为不备份,既不上传icloud
            
            if (creatSuccess) {
                
                NSError *error;
                BOOL success = [UIImageJPEGRepresentation(decodedImage, 0.5) 
    writeToFile:imageFilePath options:NSDataWritingAtomic error:&error];
                
                if (success){
                       NSLog(@"图片写入文件夹成功");
                }else{
                     NSLog(@"图片写入文件夹失败");
                       }
            }else{
                     NSLog(@"文件夹创建失败");         
                     }
      }
    }
    
    
    • 程序开发遇到的报错信息
    Error Domain=NSCocoaErrorDomain Code=512 "
    未能将文件“shareImage.png”存储到文件夹“shareImage2”中。
    " UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/
    0F51C27A-1282-4F11-8AB3-D8ACCC1A4A58/
    Documents/uzfs/A123456789/shareImage2/shareImage.png, 
    NSUnderlyingError=0x17424a2c0
    {Error Domain=NSPOSIXErrorDomain Code=21 "Is a directory"}}
    
      fileExistsAtPath:param  参数必须是个路径
    

    参考文章:http://blog.csdn.net/tpphy/article/details/48232353

    相关文章

      网友评论

          本文标题:数据写入到文件夹中

          本文链接:https://www.haomeiwen.com/subject/kgxvbxtx.html