如何在webview中显示图像? [英] How to show image in webview?

查看:116
本文介绍了如何在webview中显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在UIWebView中显示图像.但是我不知道为什么我的图像没有显示在UIWebVeiw中.这是我的代码.

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

现在将图像传递到WebViewController之后,我的WebViewController代码是

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.

任何帮助都会得到帮助.谢谢

Any help will be appriated.Thanks

推荐答案

尝试一下...

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

如果您要上传本地图像,请先将您的imagePath转换为url,然后尝试此操作,但要在我放置评论的地方写上转换url的代码,然后再传递该url……希望对您有帮助......

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......

这篇关于如何在webview中显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆