Search code examples
objective-cmacostemporary

Writing to temporary directory does not work


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.


Solution

  • Path for writing an image is incorrect:

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

    OR

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