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];
Related
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];
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];
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
In iOS, I need to load a website in uiwebview and I want to add a local image at the beginning of the webpage.
I added < img src="logo.png"/> after < body > tag but it only showed a null pic(a small white box).
How can I load my local image as the src of the img inside the html? Thanks
Using relative paths or file: paths to refer to images does not work with UIWebView. Instead you have to load the HTML into the view with the correct baseURL:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
You can then refer to your images like this:
(from uiwebview revisited)
You have to define the extact path of the image source see code below
NSString *imagePath=[[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"];
imagePath=[NSString stringWithFormat:#"file://%#",imagePath];
NSString *html=[NSString stringWithFormat:#"<html><body><img src=\"%#\"/></body></html>",imagePath];
[webView loadHTMLString:html baseURL:nil];
Cheers.
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">