English 中文(简体)
提交不工作的文件
原标题:Write to file not working

我试图把我的照片中的图像合并成一个档案,并写到磁盘上。

NSMutableArray *array = [NSMutableArray arrayWithObjects: 
                         [NSData dataWithContentsOfFile:@"0.png"], 
                         [NSData dataWithContentsOfFile:@"1.png"],
                         [NSData dataWithContentsOfFile:@"2.png"],
                         nil];

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
NSErr或 *err或 = nil;
NSString *path=@"/Users/myusername/Desktop/_stuff.dat";
[data writeToFile:path options:NSDataWritingAtomic err或:&err或];

NSArray *array = [NSArray arrayWithObjects:
                  [NSImage imageNamed:@"0"], 
                  [NSImage imageNamed:@"1"], 
                  [NSImage imageNamed:@"2"], 
                  nil];

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
NSErr或 *err或 = nil;
NSString *path=@"/Users/myusername/Desktop/_stuff.dat";
[data writeToFile:path options:NSDataWritingAtomic err或:&err或];

But both produce a file that is 4KB (empty). If I NSLog the err或 it is (null). Am I making the data the wrong way?

Edit: If I open the resulting file with a text edit或, it looks like this: enter image description here

最佳回答

I wrote a quick example:
Missing: memory management / error handling / proper file handling

// Archive

NSMutableArray *array = [[NSMutableArray alloc] init];

NSString * input = @"/Users/Anne/Desktop/1.png";

[array addObject:[NSData dataWithContentsOfFile:input]];
[array addObject:[NSData dataWithContentsOfFile:input]];
[array addObject:[NSData dataWithContentsOfFile:input]];

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
NSString *path = @"/Users/Anne/Desktop/archive.dat";
[data writeToFile:path options:NSDataWritingAtomic error:nil];


// Unarchive

NSMutableArray *archive = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

NSData * firstObject = [archive objectAtIndex:0];
NSString * output = @"/Users/Anne/Desktop/2.png";
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:output];
[firstObject writeToURL:fileURL atomically:YES];

You can also add NSImages to the NSMutableArray:

NSString * input = @"/Users/Anne/Desktop/1.png";
NSImage *image = [[NSImage alloc] initWithContentsOfFile: input];
[array addObject:image];

But that will significantly increase the file size.

问题回答

Response to the following comment:
So if I only need to access an image at runtime (in the archive), is there a way to access that image at an index without unarchiving the whole thing? Seems like unnecessary overhead to me.

I assume you re still struggling with this problem?
Hiding (or encrypting) app resources?

Like i mentioned earlier, combining all files into one big file does the trick.
Just make sure you remember the file-length of each file and file-order.
Then you can extract any specific file you like without reading the whole file.
This might be a more sufficient way if you only need to extract one file at the time.

速效样本:

// Two sample files
NSData *fileOne = [NSData dataWithContentsOfFile:@"/Users/Anne/Desktop/1.png"];
NSData *fileTwo = [NSData dataWithContentsOfFile:@"/Users/Anne/Desktop/2.png"];

// Get file length
int  fileOneLength = [fileOne length];
int  fileTwoLength = [fileTwo length];

// Combine files into one container
NSMutableData * container = [[NSMutableData alloc] init];
[container appendData:fileOne];
[container appendData:fileTwo];

// Write container to disk
[container writeToFile:@"/Users/Anne/Desktop/container.data" atomically:YES];

// Read data and extract sample files again
NSData *containerFile = [NSData dataWithContentsOfFile:@"/Users/Anne/Desktop/container.data"];
NSData *containerFileOne =[containerFile subdataWithRange:NSMakeRange(0, fileOneLength)];
NSData *containerFileTwo =[containerFile subdataWithRange:NSMakeRange(fileOneLength, fileTwoLength)];

// Write extracted files to disk (will be exactly the same)
[containerFileOne writeToFile:@"/Users/Anne/Desktop/1_extracted.png" atomically:YES];
[containerFileTwo writeToFile:@"/Users/Anne/Desktop/2_extracted.png" atomically:YES];

// Only extract one file from the container
NSString * containerPath = @"/Users/Anne/Desktop/container.data";
NSData * oneFileOnly = [[NSFileHandle fileHandleForReadingAtPath:containerPath] readDataOfLength:fileOneLength]; 

// Write result to disk
[oneFileOnly writeToFile:@"/Users/Anne/Desktop/1_one_file.png" atomically:YES];

Tip: You can also save the index inside the container file.
For example: The first 500 bytes contain the required information.
When you need a specific file: Read the index, get the file position and extract it.

You are archiving a NSMutable array of NSImage. This two classes conform to the NSCoding protocol required by NSKeyedArchiver, so I don t see where would be your problem.
So, here are many ideas to test.

第一,你是否确信你认为你掌握的数据是有效的? 在您的第一部法典中,请填写[NSData data WithContentsOfFile:@0.png”]。 这种方法期望有一条绝对的档案途径。

Assuming the problem is not in your code, just in your question, let s continue:

在您存档后,您在可变数据中是否有别之处? 也就是说,在数据分配之后,你可以添加这一代码。 如果这一说法失败,你将有一个例外:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
NSAssert(nil != data, @"My object data is nil after archiving");

If the problem was not here, what is the return of the line [data writeToFile:path options:NSDataWritingAtomic error:&error];
(Not the variable error, but the return value of the call to the method - writeToFile: options: error:)

如果你简化你的法典,并做些什么:

result = [NSKeyedArchiver archiveRootObject:data
                                     toFile:archivePath];

如果说什么是ok,你是否试图把你的文件编为国家档案局。

问题是,<代码>[NSData data WithContentsOfFile:@0.png”>在目前的名录中查找“0.png”文档,但申请的考虑可能不是您所期望的位置。 对于图形图象,你应始终使用一条绝对的道路或一条与你能够走到绝对道路(例如,你的 app、申请支持目录、一些用户选择的地点)相对应的道路。

For command-line tools, using the current directory is more common. But I doubt that s the case here.

Another thing I noticed on Mavericks and up is that the folders in the path must be in existence. Meaning you must create the folder structure prior to saving into that folder. If you try to write to a folder on the desktop or elsewhere, even with sandboxing off, it will fail if the folder does not exist. I know this has been answered already, but I found that my issue continued regardless, but once I make sure that the folder structure was in place, I could do my writing to that folder.

在一旁注:我相信你能够从全国人民论坛组织那里这样做。 我在我最后确定我的评估结构之后,我就这样做了,但希望这能帮助在uce中丧生的其他人。





相关问题
2 mysql instances in MAC

i recently switched to mac. first and foremost i installed xampp. then for django-python-mysql connectivity, i "somehow" ended up installing a seperate MySQL. now the seperate mysql installation is ...

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Controlling OSX windows

I m trying to control windows of a foreign OSX applications from my application. I d like to 1. move the windows on the screen 2. resize the windows on the screen 3. change the currently active window ...

Switching J2SE versions on Mac OS (SnowLeopard)

My current JDK on Mac OS (10.6) is set to 1.6 and I d like to switch to 1.5. A listing of /System/Library/Frameworks/JavaVM.framework/Versions/ shows: lrwxr-xr-x 1 root wheel 10 Nov 3 18:34 ...

Scrolling inside Vim in Mac s Terminal

I ve been googling around trying to figure out if it s possible to use my mouse wheel to scroll while inside Vim in Mac s Terminal, with no luck. It seems as if only X11 or iTerm support this. Before ...

export to MP3 from quicktime API

A question for Apple,QT programmers. Would like to know if it s possible to export a Movie object to MP3 using the QuickTime API. Preferably the ConvertMovieToFile function. I ve looked at ...

热门标签