I'm having some troubles with the bundle and somehow I can't save images from bundle to docs directory any more. Now I've got this error before building:
The document NSBundle.h could no be
saved
It apparently compiles well.
This is the kind of code I'm using:
//Get every name from plist array and append it to the full path
for( NSString *aName in namesPackage ) {
bundlePath = [[NSBundle mainBundle] pathForResource:aName ofType:#"png"];
if([fileManager fileExistsAtPath:bundlePath])NSLog(#"it exists in bundle: %#",bundlePath);
imagePath = [NSString stringWithFormat:#"%#/%#/%#",DOCUMENTS_FOLDER,package,aName];
[fileManager copyItemAtPath:bundlePath toPath:imagePath error:&anError];
if(anError) NSLog([anError description]);
}
Thanks for your help in advance.
You should use NSString's file extensions category:
-stringByAppendingPathComponent:
-stringByAppendingPathExtension:
That will take care of any potential issues with trailing slashes, etc.
Also, you should never pass any unknown string as the format argument to any variable length function, as they could contain format specifiers. Use NSLog(#"%#", anError) instead.
Related
Hi i am trying to save text values as binary file and read from that file. i am using following code, i got the binary file in documents directory but when reading data from file only got some numbers please kindly help its urgent.
For writing
NSString *documentsDirectoryPath = [self performSelector:#selector(tempDirectoryPath:) withObject:fileName_];
NSLog(#"%#",documentsDirectoryPath);
if ([[NSFileManager defaultManager] isWritableFileAtPath:documentsDirectoryPath]) {
NSLog(#"content =%#",data_);
[data_ writeToFile:documentsDirectoryPath atomically:YES];
return YES;
}
For reading i use the following code,
NSString *documentsDirectoryPath = [self performSelector:#selector(tempDirectoryPath:) withObject:fileName_];
if ([[NSFileManager defaultManager] isReadableFileAtPath:documentsDirectoryPath]) {
NSMutableData *data_ = [NSMutableData dataWithContentsOfFile:documentsDirectoryPath];
return data_;
}
I got only numbers from the data_ .
How to read the .bin file correctly.?
I got the .bin file when extract get .bin.cpgz file.I can't open the file what is the reason ?Is anything wrong in code?
I am pass string in this way:
[self writeData:#"test string is here" toFile:#"mf.bin"];
Thanks.
It's a little late, but something like this might help you:
http://snippets.aktagon.com/snippets/475-How-to-use-NSKeyedArchiver-to-store-user-settings-on-the-iPhone
Sounds like you need to convert the data into something readable.
NSString *myFile = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
This assumes originally the data you wrote was a NSString, if it was an object you would have to use the appropriate methods for that object.
Instead of loading a PDF from the resources folder I would like to
load it from the documents directory. I have been trying to do this
for days but the CGPDFDocumentRef keeps returning NULL. Here is my
code:
// Get Documents Directory
NSArray *searchPaths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [searchPaths objectAtIndex:0];
NSString *tempPath = [documentsDirectoryPath
stringByAppendingPathComponent:[appDelegate.issueToLoad
stringByAppendingPathExtension:#"pdf"]];
NSString *path = [tempPath
stringByReplacingOccurrencesOfString:#"localhost/" withString:#""];
NSLog(#"PATH: %#", path);
//Display PDF
CFURLRef pdfURL = CFURLCreateWithFileSystemPath (NULL,
(CFStringRef)path, kCFURLPOSIXPathStyle, FALSE);
NSLog(#"PDF URL: %#", pdfURL);
pdf = CGPDFDocumentCreateWithURL(finalURL);
NSLog(#"PDF: %#", pdf);
The file path is correct and I have checked the simulator documents
directory and the file is definitely there. When I run the app the
last NSLog says (NULL) and a blank white page PDF is displayed.
Any ideas what is wrong?
Thanks!
The obvious answer would be to use the pdfURL in der DocumentCreate function. finalURL comes out of nowhere and it's not apparent what it's even there for.
pdf = CGPDFDocumentCreateWithURL(pdfURL);
ok, next try. I hope this time it will be more useful :-/
are you sure the file exists at this path?
I created a testcase similar to yours. And with a file named "file 123.pdf" this seems to work. At least I can read the version of the pdf.
I added this after your code sample to see if the pdf was loaded.
NSLog(#"PDF: %#", pdf);
int majorVersion;
int minorVersion;
CGPDFDocumentGetVersion(pdf, &majorVersion, &minorVersion);
NSLog(#"%d %d", majorVersion, minorVersion);
and this gives me the following console output:
2010-10-08 13:01:40.246 test[3517:207] PATH: /var/mobile/Applications/E9CDCAC1-430D-488E-ABC3-33F40F6A06F4/Documents/file 123.pdf
2010-10-08 13:01:40.252 test[3517:207] URL: file://localhost/var/mobile/Applications/E9CDCAC1-430D-488E-ABC3-33F40F6A06F4/Documents/file%20123.pdf
2010-10-08 13:01:40.257 test[3517:207] PDF: <NSCFType: 0x139660>
2010-10-08 13:01:40.260 test[3517:207] 1 4
there is clearly a %20 inside, so I think this is not the problem with your implementation.
EDIT:
Add this to your code to make sure that the file exists at this path.
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
NSLog(#"File does not exist!");
There must be a valid pdf-File at your path.
You probably don't have a PDF file at that path. The “Create” in CGPDFDocumentCreateWithURL refers to the CGPDFDocument object; it will not create a document file at that URL. You can only create a CGPDFDocument object for a PDF file that already exists.
Also, as I mentioned on your other question, I don't see why you're trying to delete “localhost/” from the path. It's unlikely to exist there in the first place (it's more likely to only appear in the URL), and if it ever does appear there, it should appear there, and stripping it out will make the path wrong.
Instead of loading a PDF from the resources folder I would like to
load it from the documents directory. I have been trying to do this
for days but the CGPDFDocumentRef keeps returning NULL. Here is my
code:
// Get Documents Directory
NSArray *searchPaths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [searchPaths objectAtIndex:0];
NSString *tempPath = [documentsDirectoryPath
stringByAppendingPathComponent:[appDelegate.issueToLoad
stringByAppendingPathExtension:#"pdf"]];
NSString *path = [tempPath
stringByReplacingOccurrencesOfString:#"localhost/" withString:#""];
NSLog(#"PATH: %#", path);
//Display PDF
CFURLRef pdfURL = CFURLCreateWithFileSystemPath (NULL,
(CFStringRef)path, kCFURLPOSIXPathStyle, FALSE);
NSLog(#"PDF URL: %#", pdfURL);
pdf = CGPDFDocumentCreateWithURL(finalURL);
NSLog(#"PDF: %#", pdf);
The file path is correct and I have checked the simulator documents
directory and the file is definitely there. When I run the app the
last NSLog says (NULL) and a blank white page PDF is displayed.
Any ideas what is wrong?
Thanks!
The obvious answer would be to use the pdfURL in der DocumentCreate function. finalURL comes out of nowhere and it's not apparent what it's even there for.
pdf = CGPDFDocumentCreateWithURL(pdfURL);
ok, next try. I hope this time it will be more useful :-/
are you sure the file exists at this path?
I created a testcase similar to yours. And with a file named "file 123.pdf" this seems to work. At least I can read the version of the pdf.
I added this after your code sample to see if the pdf was loaded.
NSLog(#"PDF: %#", pdf);
int majorVersion;
int minorVersion;
CGPDFDocumentGetVersion(pdf, &majorVersion, &minorVersion);
NSLog(#"%d %d", majorVersion, minorVersion);
and this gives me the following console output:
2010-10-08 13:01:40.246 test[3517:207] PATH: /var/mobile/Applications/E9CDCAC1-430D-488E-ABC3-33F40F6A06F4/Documents/file 123.pdf
2010-10-08 13:01:40.252 test[3517:207] URL: file://localhost/var/mobile/Applications/E9CDCAC1-430D-488E-ABC3-33F40F6A06F4/Documents/file%20123.pdf
2010-10-08 13:01:40.257 test[3517:207] PDF: <NSCFType: 0x139660>
2010-10-08 13:01:40.260 test[3517:207] 1 4
there is clearly a %20 inside, so I think this is not the problem with your implementation.
EDIT:
Add this to your code to make sure that the file exists at this path.
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
NSLog(#"File does not exist!");
There must be a valid pdf-File at your path.
You probably don't have a PDF file at that path. The “Create” in CGPDFDocumentCreateWithURL refers to the CGPDFDocument object; it will not create a document file at that URL. You can only create a CGPDFDocument object for a PDF file that already exists.
Also, as I mentioned on your other question, I don't see why you're trying to delete “localhost/” from the path. It's unlikely to exist there in the first place (it's more likely to only appear in the URL), and if it ever does appear there, it should appear there, and stripping it out will make the path wrong.
Instead of loading a PDF from the resources folder I would like to
load it from the documents directory. I have been trying to do this
for days but the CGPDFDocumentRef keeps returning NULL. Here is my
code:
// Get Documents Directory
NSArray *searchPaths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [searchPaths objectAtIndex:0];
NSString *tempPath = [documentsDirectoryPath
stringByAppendingPathComponent:[appDelegate.issueToLoad
stringByAppendingPathExtension:#"pdf"]];
NSString *path = [tempPath
stringByReplacingOccurrencesOfString:#"localhost/" withString:#""];
NSLog(#"PATH: %#", path);
//Display PDF
CFURLRef pdfURL = CFURLCreateWithFileSystemPath (NULL,
(CFStringRef)path, kCFURLPOSIXPathStyle, FALSE);
NSLog(#"PDF URL: %#", pdfURL);
pdf = CGPDFDocumentCreateWithURL(finalURL);
NSLog(#"PDF: %#", pdf);
The file path is correct and I have checked the simulator documents
directory and the file is definitely there. When I run the app the
last NSLog says (NULL) and a blank white page PDF is displayed.
Any ideas what is wrong?
Thanks!
The obvious answer would be to use the pdfURL in der DocumentCreate function. finalURL comes out of nowhere and it's not apparent what it's even there for.
pdf = CGPDFDocumentCreateWithURL(pdfURL);
ok, next try. I hope this time it will be more useful :-/
are you sure the file exists at this path?
I created a testcase similar to yours. And with a file named "file 123.pdf" this seems to work. At least I can read the version of the pdf.
I added this after your code sample to see if the pdf was loaded.
NSLog(#"PDF: %#", pdf);
int majorVersion;
int minorVersion;
CGPDFDocumentGetVersion(pdf, &majorVersion, &minorVersion);
NSLog(#"%d %d", majorVersion, minorVersion);
and this gives me the following console output:
2010-10-08 13:01:40.246 test[3517:207] PATH: /var/mobile/Applications/E9CDCAC1-430D-488E-ABC3-33F40F6A06F4/Documents/file 123.pdf
2010-10-08 13:01:40.252 test[3517:207] URL: file://localhost/var/mobile/Applications/E9CDCAC1-430D-488E-ABC3-33F40F6A06F4/Documents/file%20123.pdf
2010-10-08 13:01:40.257 test[3517:207] PDF: <NSCFType: 0x139660>
2010-10-08 13:01:40.260 test[3517:207] 1 4
there is clearly a %20 inside, so I think this is not the problem with your implementation.
EDIT:
Add this to your code to make sure that the file exists at this path.
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
NSLog(#"File does not exist!");
There must be a valid pdf-File at your path.
You probably don't have a PDF file at that path. The “Create” in CGPDFDocumentCreateWithURL refers to the CGPDFDocument object; it will not create a document file at that URL. You can only create a CGPDFDocument object for a PDF file that already exists.
Also, as I mentioned on your other question, I don't see why you're trying to delete “localhost/” from the path. It's unlikely to exist there in the first place (it's more likely to only appear in the URL), and if it ever does appear there, it should appear there, and stripping it out will make the path wrong.
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];