Writing to a file does not work

I am trying to combine the images in my application into a single file and burn it to disk.

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

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

or

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

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

But both files create a 4 KB file (empty). If I am a NSLogmistake, this (null). Am I doing data in the wrong way?

Edit: if I open the resulting file using a text editor, it will look like this: enter image description here

+3
source share
5 answers

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

// 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 NSMutableArray:

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

But this will significantly increase the file size.

+4
source

:
, ( ), ? .

, ?
( ) ?

, .
, .
, , .
, .

"" :

// 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];

: "" .
: 500 .
: , .

+4

NSMutable NSImage. NSCoding, NSKeyedArchiver, , .
, .

-, , , , , , ? [NSData dataWithContentsOfFile:@"0.png"]. .

, , , :

- ? .. . , :

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

, [data writeToFile:path options:NSDataWritingAtomic error:&error];
( , - writeToFile: options: error:)

, :

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

, NSKeyedUnarchiver?

+1

, [NSData dataWithContentsOfFile:@"0.png"] "0.png" , , , , , . , , (, , , - ).

. , .

+1

, Mavericks, - , . . , , , . , , , , , , .

On the side of the note: I'm sure you can do this from NSFileManager, and I will do it myself as soon as I complete my application structure, but hope this helps someone else lose in the sauce.

0
source

Source: https://habr.com/ru/post/1795801/


All Articles