How to get file size of pdf,gif,doc etc using xcode.
suppose to i get pdf file form resorce folder so how can i get size of for this file.
is there any way?
Thanks you,
If you want to calculate the size of your resource file in run-time basically you can do the following (omitting error checks etc):
// Get path for resource file
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:#"myPDFFile" ofType:#"pdf"];
// Get file attributes
NSDictionary* attributeDict = [[NSFileManager defaultManager] attributesOfItemAtPath:resourcePath error:nil];
// Get size attribute
NSNumber* fileSizeObj = [attributeDict objectForKey:NSFileSize];
long long fileSizeVal = [fileSizeObj lonLongValue];
Related
My app has some content dumping in document folder periodically. And i have used the below code to calculate my total app consumed memory size. But i see its different from what i see from settings\usage. My code shows very less memory. I am not able to track where i am going wrong in calculating/ or have i missed folder paths.
I have calculated my app size as follows:
NSString *folderPath = [[NSBundle mainBundle] bundlePath];
NSArray *filesArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:folderPath error:nil];
NSEnumerator *filesEnumerator = [filesArray objectEnumerator];
NSString *fileName;
unsigned long long int fileSize = 0;
while (fileName = [filesEnumerator nextObject]) {
NSDictionary *fileDictionary = [[NSFileManager defaultManager] attributesOfItemAtPath: [folderPath stringByAppendingPathComponent:fileName] error:nil];
fileSize += [fileDictionary fileSize];
};
The above code returns the actual file size. The value shows in the settings/usage shows the allocated size. The allocated size shall be more than the actual size.
That's why your calculated value and the setting/usage values are not matching.
If you need to access the allocated size of a file please use NSURLTotalFileAllocatedSizeKey in NSURL class. There is no methods/ property available in NSFileManager class to get the allocated size [As far as i know].
Please go through details of File Property Keys of the NSURL class.
FYI: We shall get permission error if you try to access the /Library/Caches/Snapshot folder in Home directory of sanboxed app.
This may be helpful to you.
I saved a movie file's path from UIImagePickerController, and I know it exists because I can play it on the device. An NSLog on the string containing the movie file path returns this:
file://localhost/private/var/mobile/Applications/E694555D-3959-4CC5-A829-4260323C2C65/tmp//trim.6JemAI.MOV
When this string is used like this however, it returns NO:
NSLog(#"file exists: %i", [[NSFileManager defaultManager] fileExistsAtPath:media.movie]);
Any idea this this is failing? Could it be related to the value being stored as a path, or perhaps that the path includes // at one point? These are just some thoughts I've had.
You need to convert the URL to a file path.
NSURL *url = info[UIImagePickerControllerMediaURL];
NSString *path = [url path];
NSLog(#"file exists: %i", [[NSFileManager defaultManager] fileExistsAtPath:path]);
A path doesn't have the leading file://localhost.
Suppose, I have added one folder name "Images" in my project.How can I get the path to that folder? My main intention is to get the number of pictures in "Images" folder.
You should work a bit more on your question: it assumes a lot and requires the reader to guess.
I have added one folder name "Images" in my project
So I guess this means you added it as a folder reference
and I want to get it's path
And I guess that you want to do that at run time from your application, not at build-time from Xcode.
If so, you could do something like:
NSURL *containingURL = [[NSBundle mainBundle] resourceURL];
NSURL *imageURL = [containingURL URLByAppendingPathComponent:#"Images" isDirectory:YES];
NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSArray *content = [localFileManager contentsOfDirectoryAtURL:imageURL includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsSubdirectoryDescendants error:NULL];
[localFileManager release];
NSUInteger imageCount = [content count];
This code does not assume that all images are of the same kind.
[[[NSBundle mainBundle] pathsForResourcesOfType:#"jpg" inDirectory:#"Images"] count];
This returns the number of jpg images from the Images folder. This is the case if you added the images to your application bundle.
I want create and return a dictionary using the keys and values found in a file specified by a given path. I have my file on my Desktop:ciudades.txt (a human readable file!!! no a xml, just for practice). What method of my NSString i need to use and how? Please can somebody help me filling on my code the XXXXXXX. Thanks in advance
- (NSMutableDictionary)ciudades
{
if (!ciudades) {
NSString *path = [NSString XXXXXXXXX];
ciudades = [NSMutableDictionary dictionaryWithContentsOfFile:path];
}
Define a function.
-(NSMutableDictionary*) ReadFileAsDictionaryForPath:(NSString* ) path
{
return [NSMutableDictionary dictionaryWithContentsOfFile:path];
}
Use it as below
NSString *path = [[NSBundle mainBundle] pathForResource:#"MyFile" ofType:#"txt"];
NSMutableDictionary* myDictionary = [self ReadFileAsDictionaryForPath:path];
if(!myDictionary)
{
NSLog(#"Error while reading data from file at path:%#",path);
}
First add that file to your application bundle By adding a existing file from xcode to your project. Then use this method to get the file path for example I'm getting a image's path.
NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:#"png"];
then try the dictionaryWithContentsOfFile method and see if it works or not.
I am saving some images in to the documents directory of the app using the following code.
for (int k=0;k<[_imageNames count];k++)
{
NSString *imagePath = [dataPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%d.jpg",k]];
NSData *data = UIImageJPEGRepresentation([_imageNames objectAtIndex:k], 1.0f);
[data writeToFile:imagePath atomically:YES];
}
Now I am trying to load the images sequentially into an NSMutableArray.
Can someone point me towards the right direction.
Try using the name of the file, iterating through and checking if the file exists:
[[NSFileManager defaultManager] fileExistsAtPath:imagePath]
When it returns NO then you know there are no more files to add.