I am saving a video to the document directory. When I take a video with the app, the amount of space the app takes up goes up, but then when I delete the video from the document directory, the amount of space the app takes up only goes down half way. For example the app takes up 1.2MB and when I take a video, it goes to 20MB. If I delete the video, the app only goes down to 10MB, not all the way back to 1.2MB as it should.
Here is the code I am using to save the video:
NSData *videoData = [NSData dataWithContentsOfURL:self.videoURL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *tempPath = [documentsDirectory stringByAppendingFormat:#"/vid1.mp4"];
BOOL success = [videoData writeToFile:tempPath atomically:NO];
And here is the code I am using to delete the video:
//Delete Video
NSError *error2 = nil;
//NSData *videoData = [NSData dataWithContentsOfURL:self.finalURL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *tempPath = [documentsDirectory stringByAppendingFormat:#"/vid1.mp4"];
BOOL success = [[NSFileManager defaultManager] removeItemAtPath: tempPath error: &error2];
if(!success)
{
NSLog(#"error from removing item at path %# is %#",
tempPath, [error2 localizedDescription]);
abort();
} else {
NSLog(#"Video deleted");
}
Related
I have the following code, I don't want to get into why I am doing it this way, but for some reason this is not working. The stringURL is working fine, it gets data back, but fails to write to the document directory. This is the first time I'm working with files, and have been pulling my hair out trying to get this to work. Please could someone point me in the right direction?
+ (void) downloadAndStorePDFFromURLWithString: (NSString *) stringURL andFileID: (NSString *) fileID andTitle: (NSString *) title;
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *pdfData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: stringURL]];
dispatch_async(dispatch_get_main_queue(), ^(void) {
//STORE THE DATA LOCALLY AS A PDF FILE
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat: #"%#/%#", fileID, title]];
//GET LOCAL FILE PATH OF DOWNLOADED PDF
//NSLog(#"SUCCESSFULLY DOWNLOADED DOCUMENT FOR FILE: %# WILL BE STORED AT %#", fileID, documentsDirectory);
BOOL success = [pdfData writeToFile: documentsDirectory atomically: YES];
NSLog(success ? #"Yes" : #"No");
//TELL TABLEVIEW TO RELOAD
//[[NSNotificationCenter defaultCenter] postNotificationName: #"DocumentDownloaded" object: nil];
//SAVE FILEPATH URL IN NSUSERDEFAULTS
//[PDFDownloadManager addURLToListOfSavedPDFs: [PDFDownloadManager filePath: fileID andTitle: title] andFileID: fileID];
});
});
}
You are attempting to write the file to a subfolder of the Documents folder. This is failing because the subfolder doesn't exist. You need to create the folder before you can write to it.
You should also clean up the code a bit. And use the better NSData method to write the file.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *folder = [documentsDirectory stringByAppendingPathComponent:fileID];
[[NSFileManager defaultManager] createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:nil error:nil];
NSString *filePath = [folder stringByAppendingPathComponent:title];
NSError *error = nil;
BOOL success = [pdfData writeToFile:filePath options: NSDataWritingAtomic error:&error];
if (!success) {
NSLog(#"Error writing file to %#: %#", filePath, error);
}
I need to save all of audio and video url's into document directory which comes from the server. i have tried a sample code
NSData *urlData = [NSData dataWithContentsOfURL:[NSURL URLWithString: "http://www.ebookfrenzy.com/ios_book/movie/movie.mp4"]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath_ = [documentsDirectory stringByAppendingPathComponent:#"/MyFolder"];
NSString *newStr = [#"test" stringByAppendingString:[NSString stringWithFormat:#".mp4"]];
NSString *FilePath = [NSString stringWithFormat:#"%#/%#",dataPath_,newStr];
[urlData writeToFile:FilePath atomically:YES];
The folder is creating in document directory but the video is not saving into the folder.Can anyone help to sort this out.
You should build the path properly and you should do some basic error checking:
NSData *urlData = [NSData dataWithContentsOfURL:[NSURL URLWithString: #"http://www.ebookfrenzy.com/ios_book/movie/movie.mp4"]];
if (urlData) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"MyFolder"];
// Create folder if needed
[[NSFileManager defaultFileManager] createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:nil];
NSString *filePath = [dataPath stringByAppendingPathComponent:#"test.mp4"];
if ([urlData writeToFile:filePath atomically:YES]) {
// yeah - file written
} else {
// oops - file not written
}
} else {
// oops - couldn't get data
}
This is the code I'm using and it works in simulator but showing nothing in idevice. If I add an folder or a file through ifunbox it shows up in device though. Do I need any permission to create or add files?
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat: #"/Folders/%#", self.txtFolder.text]];
NSLog(#"%#", dataPath);
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
[sec1Contents addObject:self.txtFolder.text];
sec1 = [NSArray arrayWithArray:sec1Contents];
[self.tblFiles reloadData];
}
thanks in advance.
Use this code :
NSURL *allitemsImageurl =[NSURL URLWithString: url];
NSData *allItemsImageData = data1; // Load XML data from web
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectoryPath = [paths objectAtIndex:0];
NSString *st1=[ #"img_" stringByAppendingString:[NSString stringWithFormat:#"%d.png",imageCount]];
NSString *allitemsImagePath = [documentsDirectoryPath stringByAppendingPathComponent:st1];
NSLog(#"%#",allitemsImagePath);
[allItemsImageData writeToFile:allitemsImagePath atomically:TRUE];
I am trying to save images to my apps documents directory. While I am easily able to save the images to the directory, its taking about two seconds to save an image and its thumbnail. I am using the following methods to save the images using a queue.
+ (void)saveImage:(UIImage*)imageToSave withName:(NSString*)imageName toFolder:(NSString*)folderName
{
[Utils createFolderWithPath:folderName];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#/%#.png",folderName,imageName]];
NSData *imageData = UIImagePNGRepresentation(imageToSave);
[imageData writeToFile:savedImagePath atomically:NO];
}
+ (void)createFolderWithPath:(NSString *)folderPath
{
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:folderPath];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:NO attributes:nil error:&error];
}
}
is there a faster way? Since I have to save multiple images and 2 seconds per image is a lot of time!
You dont need to find the document directory every time. Just find out it one time and store it. and run your code in a thread, thread will save the image in background.
i am saving file in document directory and loading it in - (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)idx with [NSURL fileURLWithPath:self.path];
returned url is of right image. but QLPreviewController shows the last saved image.This problem is occuring only in iOS6.
here is the code:
-(void)loadAndSaveFile{
NSData *data = [NSData dataWithContentsOfURL:self.mainURL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *arr=[self.urlString componentsSeparatedByString:#"."];
NSString *str=[arr lastObject];
self.pdfPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"Preview.%#",str]];
[data writeToFile:self.pdfPath atomically:YES];
[self preview];
}
where preview is a method where i initialize QLPreviewController
and in datasource method previewItemAtIndex, i'm getting path like this
fileURL1=[NSURL fileURLWithPath:self.pdfPath];
NSLog(#"%#",fileURL1);
if ([QLPreviewController canPreviewItem:fileURL1]) {
NSLog(#"///////%#",fileURL1);
return fileURL1;
}
i've solved it by deleting that file from document directory when i close QLPreviewController
NSError *err=nil;
[[NSFileManager defaultManager] removeItemAtPath: self.pdfPath error:&err];
and now its perfectly working on iOS4.3, iOS5.1 and iOS6