0

I am trying to show image in UIWebView.But I don't know why my image is not shown in UIWebVeiw. Here is my code.

-(void)pInterest
{
UIImage *myImage;
myImage=imgView.image;
WebViewController *webViewController = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:nil];
webViewController.mypimage = myImage;
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentModalViewController:webViewController animated:YES];
}

Now after passing image to WebViewController my WebViewController code is,

- (void)viewDidLoad
{
NSData* data = UIImageJPEGRepresentation(mypimage, 1.0f);
[Base64 initialize];
NSString *strEncoded = [Base64 encode:data];
 NSMutableString *htmlString = [[NSMutableString alloc] initWithCapacity:1000];
[htmlString appendFormat:@"<html> <body>"];
[htmlString appendFormat:@"<p align=\"center\"><a href=%@ class=\"pin-it-button\" count-layout=\"horizontal\"><img border=\"0\" src=\"http://assets.pinterest.com/images/PinExt.png\" title=\"Pin It\" /></a></p>", buttonUrl];
[htmlString appendFormat:@"<p align=\"center\"><img width=\"400px\" height = \"400px\" src=%@></img></p>", imageUrl];
[htmlString appendFormat:@"<script type=\"text/javascript\" src=\"//assets.pinterest.com/js/pinit.js\"></script>"];
[htmlString appendFormat:@"</body> </html>"];
[mywebview setBackgroundColor:[UIColor clearColor]];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[mywebview loadHTMLString:htmlString baseURL:baseURL];
[mywebview setOpaque:NO];
}

when I tried with this code it show below result.

enter image description here

Any help will be appriated.Thanks

Gajendra K Chauhan
  • 3,387
  • 7
  • 40
  • 55
jamil
  • 2,419
  • 3
  • 37
  • 64

1 Answers1

3

Try this...

       - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
  [imagePicker dismissModalViewControllerAnimated:YES];

   image= [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    savedImagePath = [[documentsDirectory stringByAppendingPathComponent:@"savedImage.png"] mutableCopy];
    NSLog(@"savedImagePath..%@",savedImagePath);

    // imageView is my image from camera
    imageData = UIImagePNGRepresentation(image);
    [imageData writeToFile:savedImagePath atomically:YES];



}

    - (NSString*) generatePinterestHTML {


     NSString *description = @"Post your description here";

    NSString *stringUrl = [NSString stringWithFormat:@"%@",savedImagePath];
    NSLog(@"stringUrl:%@", stringUrl);
//    
   NSURL *urlToUpload = [[NSURL alloc]initFileURLWithPath:stringUrl];

   sUrl = [NSString stringWithFormat:@"%@",urlToUpload];

 // sUrl = [NSString stringWithFormat:@"http://4.bp.blogspot.com/-w4oTZjlpgwo/T5_pi-KJPuI/AAAAAAAAAoM/rKm3E0XCbgY/s1600/red_rose_flower3.jpg"];
    NSLog(@"URL:%@", sUrl);
    NSString *protectedUrl = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(__bridge CFStringRef)sUrl, NULL, (CFStringRef)@"!'\"();:@&=+$,/?%#[]% ",CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    NSLog(@"Protected URL:%@", protectedUrl);
    NSString *imageUrl = [NSString stringWithFormat:@"\"%@\"", sUrl];
    NSString *buttonUrl = [NSString stringWithFormat:@"\"http://pinterest.com/pin/create/button/?url=www.flor.com&media=%@&description=%@\"", protectedUrl, description];

    NSMutableString *htmlString = [[NSMutableString alloc] initWithCapacity:1000];
    [htmlString appendFormat:@"<html> <body>"];
    [htmlString appendFormat:@"<p align=\"center\"><a href=%@ class=\"pin-it-button\" count-layout=\"horizontal\"><img border=\"0\" src=\"http://assets.pinterest.com/images/PinExt.png\" title=\"Pin It\" /></a></p>", buttonUrl];
    [htmlString appendFormat:@"<p align=\"center\"><img width=\"400px\" height = \"400px\" src=%@></img></p>", imageUrl];
    [htmlString appendFormat:@"<script type=\"text/javascript\" src=\"//assets.pinterest.com/js/pinit.js\"></script>"];
    [htmlString appendFormat:@"</body> </html>"];
    return htmlString;
}

- (void) postToPinterest {
    NSString *htmlString = [self generatePinterestHTML];
    NSLog(@"Generated HTML String:%@", htmlString);
    webViewPinterest.backgroundColor = [UIColor clearColor];
    webViewPinterest.opaque = NO;
    if ([webViewPinterest isHidden]) {
        [webViewPinterest setHidden:NO];
    }
    [webViewPinterest loadHTMLString:htmlString baseURL:nil];
    //[webViewPinterest loadHTMLString:@"<img src=images.png>" baseURL:nil];
}

And if you want to upload local image then first convert your imagePath into url and then try this but write your code of converting url where i put comments and then pass that url...... Hope it will helpful for you......

Sudha Tiwari
  • 2,499
  • 26
  • 50
  • let me check beacuse for this method i need to use delegate method to call generatePinterestHTML beacuse i catch image from mainclasss to WebViewController and then from WebViewController i am posting image on pinterest. – jamil Feb 07 '13 at 11:52
  • i think this method will solved my problem.but still there is some confusion ,how i can pass my mypimage instance in this url.please looks my above code and suggest any solution. – jamil Feb 07 '13 at 12:08
  • NSData* data = UIImageJPEGRepresentation(mypimage, 1.0f); [Base64 initialize]; NSString *strEncoded = [Base64 encode:data]; NSString *stringUrl = [NSString stringWithFormat:@"%@",strEncoded]; NSLog(@"stringUrl:%@", stringUrl); NSURL *urlToUpload = [[NSURL alloc]initFileURLWithPath:stringUrl]; NSString *sUrl = [NSString stringWithFormat:@"%@",urlToUpload]; i tried with these lines but still webview is blank.but when i use your code it show flower image there. – jamil Feb 07 '13 at 12:17
  • my saved image path is this savedImagePath = [[documentsDirectory stringByAppendingPathComponent:@"savedImage.png"] mutableCopy]; NSLog(@"savedImagePath..%@",savedImagePath); NSString *stringUrl = [NSString stringWithFormat:@"%@",savedImagePath]; NSLog(@"stringUrl:%@", stringUrl); // NSURL *urlToUpload = [[NSURL alloc]initFileURLWithPath:stringUrl]; sUrl = [NSString stringWithFormat:@"%@",urlToUpload]; // imageView is my image from camera imageData = UIImagePNGRepresentation(image); [imageData writeToFile:savedImagePath atomically:YES]; – Sudha Tiwari Feb 07 '13 at 12:31
  • I choose my image from photo gallery of simulator – Sudha Tiwari Feb 07 '13 at 12:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24121/discussion-between-bob-apple-and-sudha) – jamil Feb 07 '13 at 12:36