how to load the local image using lazyimageview - iphone

im using LazyImageView open source class to load the image
albumArtImgV.frame = CGRectMake(1, 1, bImgSize, bImgSize);
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"disc" ofType:#"png"];
NSLog(#"disc path..%#",filePath);
[albumArtImgV loadImageFromURL: [NSURL URLWithString:filePath]];
[albumArtImgV loadImageFromURL:[NSURL URLWithString:someurl]];
in the above code im loading the local image, later i am loading the image from network.
my intention is if there is no image in the network my local image will be displayed.
but its not loading the local image. all other network images are loading.
1>> i found that.. the url for the local image path is 'nil'. how to make a nsurl with the local bunlde path of the file.?
2>>
filePath = [filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
now the url is not nil.. though its not loading.
thanks.

[albumArtImgV loadImageFromURL: [NSURL URLWithString:filePath]];
[albumArtImgV loadImageFromURL:[NSURL URLWithString:someurl]];
being called one after the other replaces the directive to load local image. I am not aware of the source code you are referring to but this should work
albumArtImgV.image = [UIImage imageNamed:#"disc.png"];
[albumArtImgV loadImageFromURL:[NSURL URLWithString:someurl]];

Related

Accessing the image which is in external bundle

I created a bundle with name applausible.bundle, i placed the bundle in supporting files.This is my code
NSBundle* myBundle;
myBundle = [NSBundle bundleWithPath:#"/Library/applausible.bundle"];
NSString *path = [myBundle pathForResource:#"splash.png" ofType:nil];
[imageName setImage:[UIImage imageNamed:path]];
Here I am unable to display the image on the image view. Another way I tried like this I place the image on the image view using xib,while running the app. The output is coming like this in console
Could not load the "splash_screen high resolution.png" image referenced from a nib in the bundle with identifier "com.xxxxxxx.ImageSample". Can any one tell me where i made a mistake.Finally my goal is to display the image on the UIImageview which is in external bundle.
This may help you.
NSString bundlePath = [[NSBundle mainBundle] pathForResource:#"applausible" ofType:#"Bundle"];
NSLog(#"Bundle path is %#",bundlePath);
NSBundle myBundle;
myBundle = [NSBundle bundleWithPath:bundlePath];
NSString *path = [myBundle pathForResource:#"splash_screen high resolution" ofType:#"png"];
UIImage *image=[[UIImage alloc]initWithContentsOfFile:path];
[imageName setImage:image];
please try it out.
It may be helpful
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:#"applausible" ofType:#"Bundle"];
using this method you can get the bundle path.Then use your code from second line
myBundle = [NSBundle bundleWithPath:bundlePath];
NSString *path = [myBundle pathForResource:#"splash" ofType:n#"png"];
[imageName setImage:[UIImage imageNamed:path]];

Loading image into webview

How can I have a local image loaded into a html page in a UIWebView that is called with
[self.webView loadHTMLString:htmlSourceCode baseURL:externPageUrl];
I know I can get the image by changing the baseURL. However, in my case, that is not possible.
I also tried doing it by adding file://pathToImage/myimage.png to the html, but that doesn't work without changing the baseURL.
Is there any way without having to handle the shouldStartLoadWithRequest method?
Just set the image src to the name of the image file, assuming it is in the same folder as the local HTML file.
I solved it by loading the image bytes into the html directly:
//retrieve image data, to embedd into the html as base64
NSURL *imgURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"myImage" ofType:#"png"] isDirectory:NO];
NSData *data = [NSData dataWithContentsOfURL:imgURL];
[htmlContent appendFormat:#"<div class=\"myCssClass\" style=\"background:url(data:image/png;base64,%#) no-repeat center center;\" ></div>",
[data encodeBase64] ];

Loading webpage into UIwebview but images from local

I have web page having 1000's of images and values. Whenever I load the page to UIWebView it gets loaded with values but as there are so many images it takes time to download.
So is there any way, I can download the page from web but images from local.
Any help is really appreciated.
Thank you,
Ankita
If you have your HTML from webserver and images at local
yourBaseURL = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[webView loadHTMLString:#"YourHTML" baseURL:yourBaseURL]
and your HTML contains name of local image stored like
Image Tag like --> imgage src='fig1.jpg width=150 height=150 align="left"
Than this should work.
Not exactly as you required though this way you can load local images in webView.
Basically:
Download HTML from server.
NSURL *URL = [[NSURL alloc] initWithString:#"http://www.example.com/page.php"];
NSError *error = nil;
NSStringEncoding encoding;
NSString *theSource = [[NSString alloc] initWithContentsOfURL:URL usedEncoding:&encoding error:&error];
Replace the file references to load images locally.
Note the double slashes, this seems important.
// Replace:
<img src="File.png">
// By something like:
<img src="file://Path//To//Resources//File.png">
Detailed information how to do this (check post by Joe D'Andrea):
Link to resources inside WebView - iPhone
Finally make the UIWebView load the HTML:
[webView loadHTMLString:htmlString baseURL:baseURL];
Now it should load the 'fresh' HTML from the server with local images.
This works quite well for me.
NSURL *myBaseURL = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *myString = [[managedObject valueForKey:#"long_presentation"] stringByReplacingOccurrencesOfString:#"/FooFolder/BarFolder" withString:[NSString stringWithFormat:#"%#/FooFolder/BarFolder", [[NSBundle mainBundle] bundlePath] ] ];
[attractionWebView loadHTMLString:myString baseURL:myBaseURL];
I had to include a replacement for the image source
img src="FooFolder/BarFolder/my_image.jpg" ...
to get the right path.

How can I get image from my resource folder?

I want to tack one image from my resource folder. I do it like this:
NSString *fullPathToFile =[[NSString alloc]init];
fullPathToFile = [[NSBundle mainBundle] resourcePath];
fullPathToFile =[fullPathToFile stringByAppendingPathComponent:#"/groupiphonessmall.png"];
Is this correct for getting an image from the resource folder, or it gives me path of "iPhone Simulator"?
I want to send this image path to an email popup. There, I'm not getting the image.
It's a bit simpler:
NSString *pathToResourceFile = [[NSBundle mainBundle] pathForResource:#"groupiphonessmall" ofType:#"png"];
Can't you use [UIImage imageNamed:#"groupiphonesmall.png"];
Also when using stringByAppendingPathComponent: you don't need to put a slash in front of the file name, it will do that for you.
I got new way to send image in email.
Tack image
Tack this image in png format and after that convert in string by base64 encoding.
You can send this base 64 image code in html formate e-mail
Here is my code:
UIImage *imageName =[UIImage imageNamed:#"groupiphonessmall.png"];
NSData *dataObj = UIImagePNGRepresentation(imageName);
NSString *imageBase64 =[self encodeBase64WithData:dataObj];
code for email:
"< img src=\"data:image/png;base64,CODEOFBASE64\ >" "

Remap UIWebView root URL to [[NSBundle mainBundle] bundleURL]

I've got some HTML and some images in my iPhone app, arranged something like:
html/
foo.html
images/
bar.png
I can get bar.png to appear in my UIWebView a couple of different ways -- either loading foo.html from an NSUrl, and walking back up the directory tree from the html directory:
<img src="../images/bar.png"/>
or by loading foo.html into a string, using loadHtmlString, and using [[NSBundle mainBundle] bundleURL] as the baseURL:
<img src="images/bar.png"/>
Both of these are kind of clumsy, though -- in the first case, if I move HTML files around I have to rejigger all the relative paths, and in the second case, I have to ignore the actual path structure of the HTML files.
What I'd like to make work is this --
<img src="/images/bar.png"/>
-- treating the bundleURL as the root of the "site". Is there any way to make this work, or am I doomed to have that translated into file:///images/bar.png and have the file not found?
Only way I can see for you to do this would be to embed a web server in your app. Matt Gallagher has a blog post on this you could start from. Alternatively, CocoaHTTPServer and Mongoose could be dropped into your project.
If I'm not mistaken, you have some files in your project bundle that you want to load in your web view. You can do it simply with these few lines of code:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"bar" ofType:#"png"];
NSURL *imageURL = [NSURL fileURLWithPath:imagePath];
I'm assuming that you have a text/html file containing the pattern for your web view. You'll need to add the image as an object there (src="%#"...) and then add the imageURL to the pattern:
NSString *path = [[NSString alloc]initWithString:[[NSBundle mainBundle]pathForResource:#"htmlPattern" ofType:#"html"]];
NSError *error;
NSString *pattern = [[NSString alloc]initWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:&error];
htmlPage = [[NSString alloc]initWithFormat:pattern,
imageURL;
webView = [[UIWebView alloc] initWithFrame:WEBVIEW_FRAME];
[webView loadHTMLString:htmlPage baseURL:[NSURL URLWithString:path]];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:pattern]]];