CGPDFDocumentCreateWithURL problem [duplicate] - iphone

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.

Related

Why might this code be giving me null?

Scroll down to EDIT 1. This top bit is irrelevant now.
This is my code:
NSMutableDictionary *dictionary = [self.media objectAtIndex:index];
NSLog(#"dictionary: %#", dictionary);
NSString *originalImagePath = [dictionary objectForKey:#"OriginalImage"];
NSLog(#"path: %#", originalImagePath);
UIImage *originalImage = [UIImage imageWithContentsOfFile:originalImagePath];
NSLog(#"original image: %#", originalImage);
return originalImage;
And my NSLog:
dictionary: {
IsInPhotoLibrary = 1;
MediaType = 0;
OriginalImage = "/var/mobile/Applications/5E25F369-9E05-4345-A0A2-381EDB3321B8/Documents/Images/E9904811-B463-4374-BD95-4AD472DC71A6.jpg";
}
path: /var/mobile/Applications/5E25F369-9E05-4345-A0A2-381EDB3321B8/Documents/Images/E9904811-B463-4374-BD95-4AD472DC71A6.jpg
original image: (null)
Any ideas why this might be coming out as null, despite everything appearing to be in place?
EDIT:
This is the code to write the image to file:
+(NSString *)writeImageToFile:(UIImage *)image {
NSData *fullImageData = UIImageJPEGRepresentation(image, 1.0f);
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Images/"];
NSString *name = [NSString stringWithFormat:#"%#.jpg", [JEntry generateUuidString]];
NSString *filePath = [path stringByAppendingPathComponent:name];
[fullImageData writeToFile:filePath atomically:YES];
NSLog(#"original image 2: %#", [UIImage imageWithContentsOfFile:filePath]);
}
This NSLog also comes out as null. So the problem lies here, most likely. Any ideas?
EDIT 2:
So, back-tracing even more now, and I've realised that it's because this fails:
[fullImageData writeToFile:filePath atomically:YES];
You can return a bool on that line, telling if it was successful or not, and it's returning NO for me. Any ideas why this might be?
EDIT 3:
The image that gets passed in is NULL. Trying to figure out where that's gone wrong now.
Almost certainly your file does not exist. Modify your code as follows to log if a file exists or not:
NSString *originalImagePath = [dictionary objectForKey:#"OriginalImage"];
NSLog(#"path: %#", originalImagePath);
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:originalImagePath];
NSLog(#"file exists: %d", fileExists);
Possible Reasons:
The file at originalImagePath does not exist.
UIImage can not initialize an image from the specified file, because the data is corrupted (maybe the data is empty or incomplete)
the file can not be accessed because of iOS file permissions (i.e. when accessing files beyond the app sandbox)
Have you checked that the image file is really placed on path in dictionary? You may use the iExplorer utility for that.
Also pay attention to the file name case, so it's extension is 'jpg' not 'JPG'.
Finally you should check, whether it's a valid image file, by opening it with some image viewer.
are you sure that the jpg file "E9904811-B463-4374-BD95-4AD472DC71A6.jpg" is there in that folder?
try to open the app file installed in the iPhone/ipad simulator clicking on yourFile.app
with ctrl key and choose to open package contents the open your folder images...
to get a fast link to your documents folder:
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSLog(#"doc:%#",docsDir);
your app should be in its parent folder

iPhone - file properties

i m creating an application which makes the iphone work as a pendrive for easy file sharing purpose.
In the first stage, i have some files(png, pdf, jpg, zip) in a directory and i made them display in the tableview in the form of mutable array. It displays in the tableView as shown below,
.DS_Store
.localized
gazelle.pdf
Hamburger_sandwich.jpg
IITD TAJ Picture 028_jpg.jpg
iya_logo_final_b&w.jpg
manifesto09-eng.pdf
RSSReader.sql
SimpleURLConnections.zip
SQLTutorial
I just want to display the name of the files and i do not want to display the extensions. I know that it is possible to extract the extensions of a file in NSFileManager. But i do not know how. Please help me to make my table view look like this
.DS_Store
.localized
gazelle
Hamburger_sandwich
IITD TAJ Picture 028_jpg
iya_logo_final_b&w
manifesto09-eng
RSSReader
SimpleURLConnections
SQLTutorial
In the second stage i have a detailedViewController which takes displays the detailed view of the file like
file size
file type
if it is a image, it should open in imageView
if it is a song, it should play it
So i need to retrive the properties like filePath, fileType, fileSize.. of each files. Please guide me with a tutorial if possible. I too do not have any idea how to convert a mutableArray to a NSString and call the methods like stringByDeletingPathExtension. Please help me. I can even send my source code if needed. If possible, guide me with some example codes and tutorial.
This should work:)
This will get all files in a directory in a NSString *parentDirectory, get its size, if image do something otherwise it assumes is a sound file
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
NSArray *filePaths = [fm contentsOfDirectoryAtPath:parentDirectory error:&error];
if (error) {
NSLog(#"%#", [error localizedDescription]);
error = nil;
}
for (NSString *filePath in filePaths) {
//filename without extension
NSString *fileWithoutExtension = [[filePath lastPathComponent] stringByDeletingPathExtension];
//file size
unsigned long long s = [[fm attributesOfItemAtPath:[parentDirectory stringByAppendingPathComponent:filePath]
error:NULL] fileSize];
UIImage *image = [UIImage imageNamed:[parentDirectory stringByAppendingPathComponent:filePath];];
//if image...
if(image){
//show it here
}
else{
//otherwise it should be music then, play it using AVFoundation or AudioToolBox
}
}
I hope you will have the file name in NSURL object, if so then you can use the following code to get just the file name and can remove the file extension from the string.
NSArray *fileName = [[fileURL lastPathComponent] componentsSeparatedByString:#"."];
NSLog(#"%#",[fileName objectAtIndex:0]);

Using NSURL when file path has whitespace [duplicate]

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.

Load PDF from documents directory - iPhone

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.

The document NSBundle.h could no be saved

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.