我似乎无法将多个图像保存到同一个文件中。我继续使用新的样本照片(例如,IMG_3997)运行它,但是图像文件保留了我使用的第一个图像的副本...尽管文件不断变大。 seekToEndOfFile似乎没有工作,或者我只是无法在文件中看到新图像。我有一组类似的代码用于将文本写入一个完美的文件。困惑!有任何线索或建议吗?
- (void)viewDidLoad
{
[super viewDidLoad];
// Since iPhone simulator doesn't have photos, load and display a placeholder image
NSString *fPath = [[NSBundle mainBundle] pathForResource:@"IMG_3997" ofType:@"jpg"];
url = [NSURL fileURLWithPath:fPath];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
// Write image to file
UIImage *image = [UIImage imageWithContentsOfFile:fPath];
NSArray *DocumentsDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [DocumentsDirectoryPath objectAtIndex:0];
NSString *filePath = [path stringByAppendingPathComponent:@"image"];
// set up filemanager
NSFileManager *fileManager = [NSFileManager defaultManager];
//Creating or writing in a file at filePath
if ([fileManager fileExistsAtPath:filePath] == NO) {
[fileManager createFileAtPath:filePath contents:nil attributes:nil];}
//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: [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)]];
[myHandle closeFile];
}