Iphone 如何将图像和文本数据顺序写入NSDocumentDirectory文件?
我试图编写代码来创建一个简单的照片日志。选择图像,将其写入文件,然后添加文本描述。下面是我的代码。我可以写图像或文本,但不能同时按顺序写。一个或另一个写在另一个上面Iphone 如何将图像和文本数据顺序写入NSDocumentDirectory文件?,iphone,ios,Iphone,Ios,我试图编写代码来创建一个简单的照片日志。选择图像,将其写入文件,然后添加文本描述。下面是我的代码。我可以写图像或文本,但不能同时按顺序写。一个或另一个写在另一个上面 - (void)viewDidLoad { [super viewDidLoad]; // Since iPhone simulator doesn't have photos, load and display a placeholder image NSString *fPath = [[NSBundl
- (void)viewDidLoad
{
[super viewDidLoad];
// Since iPhone simulator doesn't have photos, load and display a placeholder image
NSString *fPath = [[NSBundle mainBundle] pathForResource:@"IMG_1588" ofType:@"jpg"];
url = [NSURL fileURLWithPath:fPath];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
// UIImage *image = [UIImage imageNamed:@"IMG_1588.jpg"];
// Create the file to write the image
NSArray *DocumentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [DocumentsDirectoryPath objectAtIndex:0];
NSString *filePath = [path stringByAppendingPathComponent:@"test.doc"];
//Creating a file at this path
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL ok = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
if (!ok) {NSLog(@"Error creating file %@", filePath);}
else {
//Writing image to the created file
NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
// move to the end of the file to add data
[myHandle seekToEndOfFile];
// [myHandle writeData:UIImageJPEGRepresentation(image, 1.0)];
[myHandle closeFile];
}
}
// User provides a caption for the image
- (IBAction)Button:(id)sender {
NSString *caption = enterCaption.text;
NSArray *DocumentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [DocumentsDirectoryPath objectAtIndex:0];
NSString *filePath = [path stringByAppendingPathComponent:@"test.doc"];
///Creating a file at this path
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL ok = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
if (!ok) {NSLog(@"Error creating file %@", filePath);}
else {
//Writing image to the created file
NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
// move to the end of the file to add data
[myHandle seekToEndOfFile];
[myHandle writeData: [caption dataUsingEncoding:NSUTF8StringEncoding]];
[myHandle closeFile];
}
}
//Disness Keyboard
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
@end
因为您使用createFileAtPath。仅当文件不存在时才使用,否则只需打开文件即可
编辑:记录所有内容的大小,查看是否正常。谢谢。我想我太累了,看不到这个。当我使用if([[NSFileManager defaultManager]fileExistsAtPath:filePath],YES)测试文件的现有状态时,它工作正常,但是如果([[NSFileManager defaultManager]fileExistsAtPath:filePath],NO)不工作。有什么想法或建议吗。还有两件事:(1)我不知道如何将图像保存到一个可以用NSFileManager读取的文件中,建议?和(2)您知道如何使用NSFileManager将报表放入文件中,以便我可以分隔这些条目吗?附加注释。现在,当我将图像写入文件时,与图像相比,它会生成+100页的废话。我的目标是创建一个文件,该文件的图像后跟一个标题,并允许用户像打印日记一样打印该文件。为每个条目创建一个文件夹,将文本放在一个文件中,将图像放在文件夹内的另一个文件中。