如何在Pinterest上发布图片?

时间:2013-02-01 17:46:08

标签: iphone image ios6 webview

我想使用this链接在pinterest上发布图像,但我根据自己的要求更改编码一点点这里是我的MainClass代码

- (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看起来像下面的屏幕显示  enter image description here enter image description here

当我在WebViewController中为图像视图分配相同的图像实例时,它的工作imageview.image = mypimage;

所以有人可以告诉我为什么我的webveiw没有显示图像。谢谢。

3 个答案:

答案 0 :(得分: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 :(得分:7)

在发布回答之前,我要特别感谢@Sudha,他给了我一个想法,即如果不在服务器上发布图片,我们就无法直接在app上发布本地图片。因此,当我在服务器上发布图像并分配适当的URL地址到pinit按钮时,以这种方式移动的想法,然后它对我有用。干杯! 当我开始搜索有关pinterest的iOS应用程序集成时,我发现这方面的帮助和材料很少。我认为大多数开发人员使用了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. 创建一个方法,当用户点击“Pinit”按钮时调用该方法,该按钮显示带有图像的webView,您将发布的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];
    }
    

    注意:在上面的代码中将SerVerUrl替换为您自己的服务器网址。

  4. 为了使描述更清楚,我在最后提供了一些屏幕截图,以消除可能出现的任何含糊不清和误解。

    enter image description here enter image description here enter image description here

    如有任何疑问,请随时发表评论!

答案 2 :(得分: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应用程序。