2013-02-01 6 views
5

ここに私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クラスコードが

である私は thisリンクを使用してPinterestの上の画像を投稿したいが、私は自分の要件に応じて少しのコード変更
- (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ではないと言わざるを得ない。実際に質問を投稿する前に、ロードしようとしているリソースのURLが存在することを確認してください。 –

+0

最初のスクリーンショットにあるピンチボタンをクリックすると、2番目のスクリーンショットが開きます。これは、問題がないことを意味します。私のイメージは渡されません。 – 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 okと感謝...... :) –

+0

@MicROありがとう... :) –

7

答えを投稿する前に、私は私のサーバーに画像を投稿することなく、私たちはPinterestの上で直接アプリからローカルの画像を投稿することができないという考えを与えた@Sudhaに特別な感謝を支払うしたいと思います。したがって、私はサーバーに画像を投稿し、適切なURLアドレスをピン配置ボタンに割り当てたときに、このようにしてアイデアを移してくれました。乾杯! iOSアプリのpinterestとの統合についての検索を開始したとき、私はこの点についてほとんど助けと材料を見つけませんでした。私はほとんどの開発者がthisメソッドを使っていたと思いますが、これを上記の引用されたメソッドで使用した場合、pinterestに私のローカルアプリケーションのイメージは掲載されませんでした。最終的に、私は、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. 、ユーザはWebViewのが画像で、あなたが投稿してなることをすることを示しているあなたの「Pinit」ボタンをタップしたときに呼び出すメソッドを作成します。 UIWebViewの "Pinit"ボタン。独自のサーバーのURLとSERVERURLを交換して上記のコードでは:

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

    注:これは私のコードです。

説明を明確にするために、あくまでもあくまでもあいまいさや誤解を取り除くために、スクリーンショットをいくつか用意しています。任意のクエリのために

enter image description hereenter image description hereenter image description here

、コメントすること自由に感じなさい!

+1

ありがとう@Bob Apple –

+0

ボブAppleとSudha私のビューに共有画面に合う方法はありますか?Pinterestの共有のための320X480の意味? –

+0

はいあなたは@ NikunjR.Jadavを共有することができます – jamil

0

あなたは、デバイス上のPinterestのアプリを持っている必要がありますが、これについては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"]; 

を使用することができます。

+0

私はpinterestで画像をアップロードするためのURLを持っていない、どのようにpinterestでギャラリー画像をアップロードする... –

関連する問題