IOS - can't load image at root of app - ios

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

Related

Can't read PDF document using CGPDFDocumentCreateWithURL

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];

Load PDF in UIWebView and got error DiskImageCache: Could not resolve the absolute path of the old directory

I have loaded PDF in UIWebView
NSString *thePath = [[NSBundle mainBundle] pathForResource:#"ConferenceMap_2014" ofType:#"pdf"];
if (thePath) {
NSData *pdfData = [NSData dataWithContentsOfFile:thePath];
[webViewPDF loadData:pdfData MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
}
Error : DiskImageCache: Could not resolve the absolute path of the old directory.
Hi download the class file from here.
then import the "UIView+DocumentView.h"
after that get the path of particular file
NSString *thePath = [[NSBundle mainBundle] pathForResource:#"file1427464777.586564" ofType:#"pdf"];
Next call the class with particular path
[self.view makeDocumentView:thePath];

Load Image from UIwebview fail

I am using Webview to load a image file stored in my app Library Directory, first i tried use resourcePath, and bundle path
NSString * html = [[NSString alloc] initWithFormat:#"<img src=\"file://%#\"/>", filename];
[self.webView loadHTMLString:newhtml baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];
the problem is no matter what i set in the baseUrl, i can not load the image correctly , i also tried filePath:
[self.webView loadHTMLString:newhtml baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] filePath]]];
but if i set the absolute path of the image file in the html , all the things is ok, i wonder why?
Have a look at this post: Using HTML and Local Images Within UIWebView
The top answer clearly said that using file: paths to refer to images does not work with UIWebView.
All you need to do is to pass the basepath. Example:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
Then just need to reference it like this: <img src="myimage.png">

NSData not accepting escaped / UTF8 encoded URL

I am trying to create an NSData object that contains the contents of a file in my main bundle.
NSString *chordPath = [[NSBundle mainBundle] pathForResource:chordName ofType:#"png"];
NSURL *chordURL = [NSURL URLWithString:[chordPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *chordImageData = [NSData dataWithContentsOfURL:chordURL];
UIImage *chordImage = [UIImage imageWithData:chordImageData];
Prior to using stringByAddingPercentEscapesUsingEncoding I was getting a nil URL, so I went ahead and fixed that by reencoding the string. Now I get a valid URL but the chordImageData object is nil for me. The file is definitely included in my main bundle (since I was able to get the URL to begin with) so I am wondering what's wrong.
EDIT:
Running this:
NSData *chordImageData = [NSData dataWithContentsOfURL:chordURL options:NSDataReadingMapped error:&dataCreationError];
Gets me this for the error:
po dataCreationError
Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x7530a00
Looking around Google it seems that the URL is still not encoded correctly. Anyone know an additional step to make sure the encoding is valid?
Try using fileURLWithPath:, something like this:
NSString *chordPath = [[NSBundle mainBundle] pathForResource:chordName ofType:#"png"];
NSURL *chordURL = [NSURL fileURLWithPath: chordPath];
NSData *chordImageData = [NSData dataWithContentsOfURL:chordURL];
UIImage *chordImage = [UIImage imageWithData:chordImageData];
There should be no need to do percent escaping. The reason you're having problems is that you are using URLWithString: which is meant for "non-file" URLs.
You should use +fileURLWithPath: for file-based URLs:
NSString *chordPath = [[NSBundle mainBundle] pathForResource:chordName ofType:#"png"];
NSURL *chordURL = [NSURL fileURLWithPath:chordPath];
NSData *chordImageData = [NSData dataWithContentsOfURL:chordURL];
UIImage *chordImage = [UIImage imageWithData:chordImageData];

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