Can't read PDF document using CGPDFDocumentCreateWithURL - ios

I'm, unsuccessfully, trying to load a PDF on iOS, when debugging the code the document shows "0x0" as value.
NSString *appFolderPath = [[NSBundle mainBundle] resourcePath];
NSString *pdfPath = [appFolderPath stringByAppendingString:#"/Data/Raw/test.pdf"];
CFStringRef cfsPath = (__bridge CFStringRef)pdfPath;
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, cfsPath, kCFURLPOSIXPathStyle, 0);
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL(url);
What could be wrong with this snippet? I don't have much experience with iOS native development and Objective-C.

First of all check if the file exists at the given location in the application bundle.
It's highly recommended to use the URL related API of NSBundle
NSURL *appFolderURL = [[NSBundle mainBundle] resourceURL];
NSURL *pdfURL = [appFolderURL URLByAppendingPathComponent:#"Data/Raw/test.pdf"];
And use the more convenient PDFDocument class of PDFKit rather than the CoreFoundation API
#import PDFKit;
PDFDocument *document = [[PDFDocument alloc] initWithURL:pdfURL];

Related

How to get device's pdfs and other files in iOS app?

I want to import data from my iPhone to my application . up till now i have successfully imported Photo Library and music , but i also want to import pdf and other documents in my iOS app? How can i do that?
NSString *bundlePath = [[NSBundle mainBundle] resourcePath];
NSFileManager *mgr = [[NSFileManager alloc] init];
NSArray *allFiles = [mgr contentsOfDirectoryAtPath:bundlePath error:NULL];
for (NSString *fileName in allFiles)
{
if ([[fileName pathExtension] isEqualToString:#"pdf"])
{
NSString *fullFilePath = [bundlePath stringByAppendingPathComponent:fileName];
// fullFilePath now contains the path to your pdf file
// DoSomethingWithFile(fullFilePath);
NSLog(#"file: %#",fullFilePath);
}
}
NSURL *url = [[NSBundle mainBundle] URLForResource:#"" withExtension: #"pdf"];
NSLog(#"File: %#",url);
You can't do it in iOS 8 (I haven't checked 9 though). You can't even enlist your app (adding corresponding URL schemes) in the sharing menu of iBooks.
You have to associate your app with the PDF types which you want to open. You can do this by adding some parameters to your Info.plist.
There's a well-answered post which explains this:
How do I associate file types with an iPhone application?
You should also consider reading Apple's documentation and a blog post written by me.

IOS - can't load image at root of app

I'm new to iOS and need to create a NSURL that points to an image that I have copied into the root of my application (i.e. same directory as the AppDelegate & my controller class files reside).
Giving the following code, why does the image not load & how can I get the if condition to validate (i.e. have a NSURL reference to my image)?
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *image2AbsolutPath = [mainBundle pathForResource:#"image2" ofType:#"jpg"];
NSURL *urlToImage = [NSURL URLWithString:image2AbsolutPath];
NSError *error = nil;
if (![urlToImage checkResourceIsReachableAndReturnError:&error]) {
NSLog(#"image2.jpg could not be reached.");
}
Edit: As per the comment; created an imageview and successfully loaded and viewed the image via; self.imageView.image=[UIImage imageNamed:#"image2.jpg"];
Only the NSURL loading seems to not work.
you mixup URLWithString and fileURLWithPath
//wrong
NSString *image2AbsolutPath = [mainBundle pathForResource:#"image2" ofType:#"jpg"];
NSURL *urlToImage = [NSURL URLWithString:image2AbsolutPath];
you use the former but pass it NOT a url string BUT a file path. Change to the latter and it would work
//works
NSString *image2AbsolutPath = [mainBundle pathForResource:#"image2" ofType:#"jpg"];
NSURL *urlToImage = [NSURL fileURLWithPath:image2AbsolutPath];
//but more simply
NSURL *urlToImage = [mainBundle URLForResource:#"image2" withExtension:#"jpg"];
To load the resource you need to use
NSURL *url = [[NSBundle mainBundle] URLForResource: #"image2" withExtension:#"jpg"];
If you look at the two different paths created with pathForResource vs the URLForResource method you will see the NSURL path is missing the required file://
pathForResource:
/Users/MacbookPro/Library/Developer/CoreSimulator/Devices/02D50AC9-5AF0-4F23-8757-1AF7D3153344/data/Containers/Bundle/Application/C5775786-E473-4048-9855-A2C6AA51BB05/MyApp.app/image2.png
vs
URLForResource:
file:///Users/MacbookPro/Library/Developer/CoreSimulator/Devices/02D50AC9-5AF0-4F23-8757-1AF7D3153344/data/Containers/Bundle/Application/C5775786-E473-4048-9855-A2C6AA51BB05/MyApp.app/image2.png

eschewing retain sysSound

Docs suggest the following but retain is not allowed when using ARC. What is a workaround, please?
// Create the URL for the source audio file. The URLForResource:withExtension: method is
// new in iOS 4.0.
NSURL *tapSound = [[NSBundle mainBundle] URLForResource: #"tap"
withExtension: #"aif"];
// Store the URL as a CFURLRef instance
self.soundFileURLRef = (CFURLRef) [tapSound retain];

Extracting data from an audio file

We have a web services function to which we are uploading an audio file into to be saved in the server. The code that I am using to create a data buffer (to pass to the web services) of the contents of the audio file is :
NSURL *soundFileURL = [[NSURL alloc] initFileURLWithPath:fnWithSlash];
audioData = [NSData dataWithContentsOfURL:soundFileURL];
NSLog(#"audiodata%d",audioData.length);
Here fnWithSlash contains the full path of the file. However audioData.length is always 0.
What am i missing?
Thanks in advance for your response.
Try using NSData's dataWithContentsOfFile: instead..If the file is in your bundle it will look like the example below, otherwise construct the filePath of the sound file to your desired location..
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"yourSound" ofType:#"wav"];
audioData = [NSData dataWithContentsOfFile:filePath];
NSLog(#"audiodata%d",audioData.length);

How to load a local picture in three20's Photo Browser

in three20's TTCatalog demo, PhotoTest1Controller.m .there's some code
[[[MockPhoto alloc]
initWithURL:#"http://ww4.sinaimg.cn/bmiddle/6e8f23a5jw1dk7pylzs8ij.jpg"
smallURL:#"http://ww4.sinaimg.cn/thumbnail/6e8f23a5jw1dk7pylzs8ij.jpg"
size:CGSizeMake(320, 480)] autorelease],
can i replace these URLs with local path.
i put the pictures into app's bundle.
File URLs (file://) should work just fine. For images in your app bundle you can use -[NSBundle URLForResource:withExtension:]:
NSURL *imageURL = [[NSBundle mainBundle] URLForResource:#"foo" withExtension:#"jpg"];
On iOS < 4.0 you must combine path-based NSBundle methods with +[NSURL fileURLWithPath:]:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"foo" ofType:#"jpg"];
NSURL *imageURL = [NSURL fileURLWithPath:imagePath];

Resources