如何在webview中显示图像?

时间:2013-02-07 11:17:48

标签: iphone image uiwebview base64 xcode4.5

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

-(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代码是

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

当我尝试使用此代码时,它会显示以下结果。

enter image description here

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

1 个答案:

答案 0 :(得分:3)

试试这个......

       - (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的代码,我把评论放在那里然后传递那个网址......希望它对你有所帮助......