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
Related
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
}
I have been having an issue and I can't find anything about it.
I got some code to write files in the Documents folder with iOS. Checked the Apple documentation seems it's where I want to put the file for it to be available the next launch.
Here's the code I'm using:
- (void) writeToTextFile:(NSString *)logMessage inFile:(NSString *)fileName
{
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *filePath = [NSString stringWithFormat:#"%#/%#.txt", documentsDirectory, fileName];
NSLog(#"TEST path: %#", filePath);
//save logMessage to the documents directory
[logMessage writeToFile:filePath atomically:YES encoding:NSStringEncodingConversionAllowLossy error:nil];
}
Now I'm using unit tests on this methods. It was working fine on the iOS 7.0.3 simulator, but when I switched to iOS 6.0 it doesn't work anymore.
In fact I found out that there was no Documents folder in "Application Support/iPhone Simulator/6.0/" but there's one in the 7.0.3 folder.
I can't really find any differences on how writing to a file should be handled between 6 and 7. Should I test if the folder exists then create it if it's not there?
This works for me
NSString *string = #"Text to save";
NSString *filePath = #"filerecord.txt";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:filePath];
[string writeToFile:filePath atomically:YES];
UPDATE
Create Documents directory if not exists
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (![fileManager fileExistsAtPath:documentsDirectory]) {
[fileManager createDirectoryAtPath:documentsDirectory withIntermediateDirectories:NO attributes:nil error:&error];
}
NSString *string = #"Text to save";
NSString *filePath = #"filerecord.txt";
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:filePath];
[string writeToFile:filePath atomically:YES];
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");
}
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.