2

我正在使用蓝牙将文件发送到其他设备。发送的内容是 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];

该文件是一个图像。该文件已收到并且可以正常查看,但我想将其保存到与以前相同的名称的文档目录中,即 button.titleLabel.text。当您发送文件时,它总是有一个名称。我怎么才能得到它?

4

2 回答 2

1
  • Your code

    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];
    
  • Make NSDictionary and put the filename and image data like this

    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjects:myFilePath,Recording  forKeys:@"fileName",@"imageData"];
    
  • Then convert the dictionary into NSData like this

    NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:dict];
    

  • After recieving the data convert the data into dictionary again and extract the imageData and the filePath

    NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData];
    
于 2013-09-04T14:36:09.760 回答
1

您需要将文件名作为单独的实体(可能是 NSString 或 NSData)发送,或者,由于它是图像,您可以将文件名添加到 EXIF 数据中,尽管这并不容易。

于 2013-09-04T14:24:16.530 回答