0

I have UIWebView i want to load image in that but it does not show image in it i am using following code

     pdfView.hidden=NO;
    webView.hidden=NO;
    pdfView.backgroundColor=[UIColor clearColor];

    doneButton = [[UIBarButtonItem alloc]initWithTitle:@" Done " style:UIBarButtonItemStyleBordered target:self action:@selector(doneButtonClick)];            
    self.navigationItem.leftBarButtonItem =doneButton;


    webView.backgroundColor=[UIColor clearColor];


    NSLog(@"File Name is %@",fileName);

    NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"png"];




    NSURL *url = [NSURL fileURLWithPath:fileName];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView setScalesPageToFit:YES];

    [webView loadRequest:request];      

is there any way to load this image which is in fileName which comes from sever in this webView thanks

Jaw Ali
  • 205
  • 5
  • 13
  • your path is i think not proper.what's your real path for getting images from server? – BhavikKama Jun 20 '13 at 10:12
  • http://celeritas-solutions.com/pah_brd_v1/productivo/pro/imageName.png this is the path of server getting images – Jaw Ali Jun 20 '13 at 10:16
  • check this out http://iphonedevsdk.com/forum/iphone-sdk-development/1937-uiwebview-displaying-image-from-resource-file.html – iCoder Jun 20 '13 at 10:17
  • i think you shold check with your filename..is that path coming same as the actual url.and the path u gave me its not working..so may be your path is the problem – BhavikKama Jun 20 '13 at 10:19
  • and is it necessery to load image in uiwebview? – BhavikKama Jun 20 '13 at 10:25
  • Add the image to your XCode - to add an image into XCode, create some folder say "image" and save that image in it.. Simply drag and drop the folder into the application folder. A popup window will come up just check the option that says to add the image in application folder and save it(second option, other than the copy). Finally you can see image folder in your application.Use mainbundle to get the path for that image and use html to load that image in webview. – Nuzhat Zari Jun 20 '13 at 11:44

3 Answers3

1

The other way i can go for is to html-page dynamically and load it into UIWebView:

NSString *imageUrlString = @"your url ";
NSString *yourText = @"Some text here";

NSString *htmlString = [NSString stringWithFormat:@"<img src='%@'/><br><b>%@</b>", imageUrlString, yourText];    
[myWebView loadHTMLString:htmlString baseURL:nil];
BhavikKama
  • 8,566
  • 12
  • 94
  • 164
  • so don't you think its the image URL which is not proper?because the URL u gave me its not working in my browser – BhavikKama Jun 20 '13 at 10:36
  • ok..and can you use uiimageview rather then uiwebview if you want to show only images...i have much more easy solution for that AsyncImageView which will not either store your images in local.and very fast in process – BhavikKama Jun 20 '13 at 10:42
  • please tell me that solution is there a way to check url is ok or not – Jaw Ali Jun 20 '13 at 10:43
  • just copy your url and try to paste in browser tab...if you can see that image in browser..then its working – BhavikKama Jun 20 '13 at 10:44
  • ok..now i think only problem is in urlstring ...dont worry just go to this link it will help you further http://stackoverflow.com/questions/1926117/iphone-dev-uiwebview-baseurl-to-resources-in-documents-folder-not-app-bundle – BhavikKama Jun 20 '13 at 10:49
0

I think you can tighten this up a bit. Instead of getting a string path and creating a file URL from it, just ask the bundle for a URL.

My best guess is your filename already has the file extension in it and you're not actually getting back a valid file path/url. Step through in the debugger and see.

// get the file resource URL
NSURL *url = [[NSBundle mainBundle] URLForResource: filename withExtension: @"png"];

// sanity check - did we get a valid URL?
NSAssert( url != nil && [url checkResourceIsReachableAndReturnError: NULL], @"oops - no resource!");

// load it
[webView loadRequest: [NSURLRequest requestWithURL:url]];
TomSwift
  • 39,369
  • 12
  • 121
  • 149
-1

Why can't you just use a UIImageView? This is done by hand but something like;

NSURL *url = [NSURL fileURLWithPath:fileName];
UIImage *tempImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
imageView.image = tempImage;
Recycled Steel
  • 2,272
  • 3
  • 30
  • 35