سؤال

I am trying to write a file to a temporary directory. I am doing it like this:

NSString *_location = NSTemporaryDirectory();
NSString *_guid = [[NSCalendarDate calendarDate] descriptionWithCalendarFormat:@"%m%d%Y%H%M%S%F"];
_guid = [_guid stringByAppendingString:@".png"];
NSString *_tempFilePath = [NSString stringWithFormat:@"%@/%@", _location, _guid];
NSData *_temp_data = [imgRep representationUsingType: NSPNGFileType properties: nil];
[_temp_data writeToFile:_tempFilePath atomically: NO];

But it doesn't work for me. It doesn't create the file. What's the problem?

P.S. I have tried to create a directory with a unique name in NSTemporaryDirectory, and then write to a file there, but it didn't work either.


I noticed that it don't creates a file anywhere. Tried to set location to user's documents folder, but its not working.

هل كانت مفيدة؟

المحلول

Path for writing an image is incorrect:

NSString *_tempFilePath = [NSString stringWithFormat:@"%@/%@", _location,_guid];

OR

 NSString *_tempFilePath = [location stringByAppendingPathComponent:@"%@",guid];

نصائح أخرى

If you write to a file inside potentially new directories, you have to first create the directory:

[[NSFileManager defaultManager] createDirectoryAtPath:_directoryPath
                           withIntermediateDirectories:YES
                                            attributes:nil error:nil];

[_myData writeToFile:[_directoryPath stringByAppendingPathComponent:_fileName]
          atomically:YES];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top