2013-02-07 90 views
0

我試圖在UIWebView.F中顯示圖像,但我不知道爲什麼我的圖像未顯示在UIWebVeiw中。這是我的代碼。如何在webview中顯示圖像?

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

任何幫助將是appriated.Thanks

+0

你想實現pinteres在你的項目中? –

+0

是@Sudha我想在pinterest上發佈圖片。 – jamil

回答

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,然後嘗試這個,但寫你的代碼轉換的網址,我把評論,然後通過該網址......希望它會對你有幫助......

+0

讓我檢查beacuse這個方法,我需要使用委託方法來調用generatePinterestHTML beacuse從mainclasss捕獲圖像到WebViewController,然後從WebViewController我張貼圖像上pinterest。 – jamil

+0

我認爲這種方法將解決我的問題。但仍然存在一些困惑,我如何可以將mypimage實例傳遞給此url.please看起來我的上述代碼並提出任何解決方案。 – jamil

+0

將你的圖像路徑轉換爲字符串並將該字符串轉換爲url ... –