从NSData提取文件名

用户2014474

我正在使用蓝牙将文件发送到其他设备。发送的内容是NSData:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *myFilePath = [documentsDirectoryPath stringByAppendingPathComponent: [NSString stringWithFormat:@"%@.ext", button.titleLabel.text]];

NSData *Recording = [NSData dataWithContentsOfFile:myFilePath];

该文件是图像。接收到该文件并可以正常查看,但我想将其保存到documents目录中,并使用以前的相同名称,即button.titleLabel.text。发送文件时,它始终有一个名称。我怎么才能得到它?

伊斯兰艾哈迈德|
  • 您的验证码

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    NSString *documentsDirectoryPath = [paths objectAtIndex:0];  
    NSString *myFilePath = [documentsDirectoryPath stringByAppendingPathComponent: [NSString stringWithFormat:@"%@.ext", button.titleLabel.text]];  
    NSData *Recording = [NSData dataWithContentsOfFile:myFilePath];
    
  • 使NSDictionary并像这样放置文件名和图像数据

    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjects:myFilePath,Recording  forKeys:@"fileName",@"imageData"];
    
  • 然后像这样将字典转换成NSData

    NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:dict];
    

  • 接收到数据后,再次将数据转换为字典,并提取imageData和filePath

    NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData];
    

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章