2013-02-01 135 views
5

我想用this鏈接張貼在Pinterest的形象,但我改變編碼點點根據我自己的要求,這裏是我MainClass代碼如何在Pinterest上發佈圖片?

- (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 { 
    //imageview.image=mypimage; 
    [super viewDidLoad]; 
    NSString *description = @"Post your description here"; 

    // Generate urls for button and image 
    NSString *sUrl = [NSString stringWithFormat:@"http://d30t6wl9ttrlhf.cloudfront.net/media/catalog/product/Heros/mypimage"]; 
    // 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>"]; 
    [mywebview setBackgroundColor:[UIColor clearColor]]; 
    [mywebview loadHTMLString:htmlString baseURL:nil]; 
    [mywebview setOpaque:NO]; 
} 

當我使用上面的編碼我的WebView不顯示圖像和我的WebView看起來像下面screenshow enter image description hereenter image description here

當我將相同的圖像實例分配給WebViewController中的圖像視圖時,其工作imageview.image = mypimage;

所以有人可以告訴我爲什麼我的webveiw不顯示圖像。謝謝。

+1

我應該說這不是一個合適的按鈕url。請務必在實際發佈問題之前確保您嘗試加載的資源的網址存在。 – 2013-02-01 22:22:26

+0

當我點擊pinit按鈕,它是在第一張截圖上打開第二張截圖,這意味着沒有關於url.so的問題,唯一的問題是它不能傳遞我的圖像。 – jamil

回答

8

使用此代碼......希望它能幫助你。

- (IBAction)pinit:(id)sender { 
    [self postToPinterest]; 
} 

- (IBAction)closeWebVIew:(id)sender { 
    [webViewPinterest setHidden:YES]; 
} 

- (NSString*) generatePinterestHTML { 
    NSString *description = @"Post your description here"; 
NSURL* sUrl = [NSString stringWithFormat:@"http://4.bp.blogspot.com/-w4oTZjlpgwo/T5_pi-KJPuI/AAAAAAAAAoM/rKm3E0XCbgY/s1600/red_rose_flower3.jpg"];// pass your link here with your image name 

    NSLog(@"URL:%@", sUrl); 
    NSString *protectedUrl = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(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]; 
} 

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { 
    return YES; 
} 

- (void)webViewDidStartLoad:(UIWebView *)webView { 
    [busyIndicator startAnimating]; 
} 

-(void)webViewDidFinishLoad:(UIWebView *)webView { 
    [busyIndicator stopAnimating]; 
} 
+1

將你的最後一篇文章標記爲重複,並被版主刪除,所以我在這裏賠償+ 1。請不要張貼鏈接作爲答案。 –

+0

@MicRO好的,謝謝...... :) –

+0

@MicRO謝謝... :) –

7

之前發佈一個答案,我想,要特別感謝@Sudha誰給我的想法,如果沒有在服務器上發佈圖片,我們不能從應用程序直接Pinterest上發佈局部圖像。因此,當我在服務器上發佈圖像並將適當的URL地址分配給pinit按鈕時,以這種方式移動想法,然後它對我有用。乾杯! 當我開始用pinterest搜索iOS應用程序集成時,我發現這方面的幫助和材料很少。我認爲大多數開發人員使用this方法,但是當我使用上面引用的方法時,它並沒有在pinterest上發佈我的本地應用程序圖像。最後,我想爲那些最有可能從應用程序發佈本地圖像的人發佈我的答案。

步驟

  1. 創建新的類WebViewController並添加下面的東西。

    #import <UIKit/UIKit.h> 
    
    @interface WebViewController : UIViewController 
    { 
    IBOutlet UIActivityIndicatorView *UIHelper; 
    IBOutlet UIWebView *mywebview; 
    UIImage *mypimage; 
    } 
    @property(nonatomic,retain)UIActivityIndicatorView *UIHelper; 
    @property(nonatomic,retain)UIWebView *mywebview; 
    @property(nonatomic,retain) UIImage *mypimage; 
    -(IBAction)closeClicked:(id)sender ; 
    @end 
    
  2. 現在WebViewController.m文件代碼

    #import "WebViewController.h" 
    
    @interface WebViewController() 
    
    @end 
    
    @implementation WebViewController 
    @synthesize UIHelper,mypimage,mywebview; 
    NSString *username; 
    
    -(IBAction)closeClicked:(id)sender { 
    [self dismissModalViewControllerAnimated:YES]; 
    } 
    - (void)webViewDidStartLoad:(UIWebView *)webView { 
        [UIHelper startAnimating]; 
        } 
    -(void)webViewDidFinishLoad:(UIWebView *)webView { 
    [UIHelper stopAnimating]; 
        } 
    - (void)viewDidLoad 
    { 
        username = @"j&D"; 
    [username retain]; 
    
    NSData *imageData = UIImageJPEGRepresentation(mypimage, 90); 
    NSString *urlString = @"ServerURl/cap.php"; 
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
    [request setURL:[NSURL URLWithString:urlString]]; 
    [request setHTTPMethod:@"POST"]; 
    NSString *boundary = @"---------------------------14737809831466499882746641449"; 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 
    NSMutableData *body = [NSMutableData data]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n",username] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[NSData dataWithData:imageData]]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [request setHTTPBody:body]; 
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
        NSLog(@"serverurl :%@",returnString); 
    
        NSString *htmlString = [self generatePinterestHTML]; 
        NSLog(@"Generated HTML String:%@", htmlString); 
        mywebview.backgroundColor = [UIColor clearColor]; 
        mywebview.opaque = NO; 
        if ([mywebview isHidden]) { 
        [mywebview setHidden:NO]; 
        } 
        [mywebview loadHTMLString:htmlString baseURL:nil]; 
        [super viewDidLoad]; 
        } 
        - (NSString*) generatePinterestHTML { 
         NSString *description = @"Post your description here"; 
         NSString *sUrl = [NSString stringWithFormat:@"SerVerUrl/cap/j&D.jpg"]; 
         NSLog(@"URL:%@", sUrl); 
         NSString *protectedUrl = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(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; 
         } 
         @end 
    
  3. 創建一個方法,當用戶點擊你的「PINIT」按鈕,這表明web視圖與圖像,你將發佈和調用UIWebView上的「Pinit」按鈕。這是我的代碼:

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

    注:在上面的代碼用自己的服務器URL替換SERVERURL。

末做出的描述更清晰,我已經提供了一些截圖,以消除任何含糊和可能出現的誤解。

enter image description hereenter image description hereenter image description here

對於任何疑問,請隨時發表評論!

+1

謝謝@Bob Apple –

+0

Bob Apple和Sudha有沒有什麼辦法可以在我看來適合分享屏幕意味着在320X480上共享Pinterest分享? –

+0

是的,你可以分享@ NikunjR.Jadav – jamil

0

您可以使用the Pinterest iOS SDK

Pinterest *_pinterest = [[Pinterest alloc] initWithClientId:pinterestKey]; 
[_pinterest createPinWithImageURL:@"http://placekitten.com/500/400" 
         sourceURL:@"http://placekitten.com" 
         description:@"Pinning from Pin It Demo"]; 

對於這個然而,你需要在設備上的應用Pinterest的。

+0

我沒有url上傳pinterest上的圖片,如何上傳pinterest上的圖片圖片... –