iOS关于data.bin的文件的解析、存储、读取

逄念
2023-12-01

//                解析data.bin文件,并存储图片
                if([self isFileExist:@"data.bin"]) {

//读取本地的“data.bin”二进制文件中的内容

NSData * data =[NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/data.bin",unzipPath]];
                NSLog(@"获取到的data:%@",data);
                NSArray *array = [self turnStringToDictionary:self->_downloadModel.emojiDesc];
                    static NSString *gifPath;
                for (NSDictionary *dict in array) {

//对data进行解析
                    NSData *imageData = [data subdataWithRange:NSMakeRange([[NSString stringWithFormat:@"%@",dict[@"p"]] integerValue], [[NSString stringWithFormat:@"%@",dict[@"s"]] integerValue])];
                    UIImage *image = [UIImage imageWithData:imageData];
                    NSLog(@"image:%@",image);
                    NSLog(@"解析的GIF图片数据data:%@",imageData);
                    NSString *cachesDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
                    NSString * Pathimg =[cachesDir stringByAppendingString:[NSString stringWithFormat:@"/Image/%ld",(long)self.downloadModel.ID]];
                    if (![fileManager fileExistsAtPath:Pathimg]) {
                        [fileManager createDirectoryAtPath:Pathimg withIntermediateDirectories:YES attributes:nil error:nil];
                        gifPath = [Pathimg stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.gif",dict[@"c"]]];
                        BOOL status = [imageData writeToFile:gifPath atomically:YES];
                        NSLog(@"这个文件写入存储:%@",status?@"成功":@"失败");
                        NSLog(@"读取存储的imageData:%@",imageData);
                        NSData *Data = [NSData dataWithContentsOfFile:gifPath];
                        NSLog(@"读取存储的111data:%@",Data);
                    }else{
                        gifPath = [Pathimg stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.gif",dict[@"c"]]];
                      BOOL status = [imageData writeToFile:gifPath atomically:YES];
                        NSLog(@"这个文件写入存储:%@",status?@"成功":@"失败");
                        NSLog(@"读取存储的imageData:%@",imageData);
                        NSData *Data = [NSData dataWithContentsOfFile:gifPath];
                        NSLog(@"读取存储的222data:%@",Data);
                    }
                    NSLog(@"图片路径%@",Pathimg);
                    NSFileManager *fileManager = [NSFileManager defaultManager];
                    BOOL result = [fileManager fileExistsAtPath:gifPath];
                    NSLog(@"这个文件已经存在:%@",result?@"是的":@"不存在");
                }
            }

//判断文件是否已经在沙盒中已经存在?
-(BOOL)isFileExist:(NSString *)fileName{
    NSArray *documentArray =  NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *path = [NSString stringWithFormat:@"%@/%ld",[[documentArray lastObject] stringByAppendingPathComponent:@"Preferences"],(long)self.downloadModel.ID];
    NSString *filePath = [path stringByAppendingPathComponent:fileName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL result = [fileManager fileExistsAtPath:filePath];
    NSLog(@"这个文件已经存在:%@",result?@"是的":@"不存在");
    return result;
}

特别需要注意的是:使用writeToFile: atomically:这个API需注意保存的数据格式(数组、字典、字符串等等数据不能为nil)和路径(整个完整路径不能全部采用stringByAppendingString:进行拼接,最后文件名必需采用stringByAppendingPathComponent:进行路径拼接)否则会导致数据无法写入文件进行存储

 类似资料: