5

我想使用 链接在 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 看起来像下面的屏幕显示 在此处输入图像描述 在此处输入图像描述

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

所以有人可以建议我为什么我的 webveiw 不显示图像。谢谢。

4

3 回答 3

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];
}
于 2013-02-08T09:46:37.020 回答
7

在发布答案之前,我要特别感谢@Sudha,他让我知道如果不在服务器上发布图片,我们就不能直接在 pinterest 上发布来自应用程序的本地图片。因此,当我在服务器上发布图像并将适当的 URL 地址分配给 pinit 按钮时,以这种方式移动想法,然后它对我有用。干杯! 当我开始搜索与 pinterest 的 iOS 应用集成时,我发现这方面的帮助和材料非常少。我认为大多数开发人员都使用了这种方法,但是当我使用上面引用的方法时,它并没有在 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 替换为您自己的服务器 url。

为了使描述更清晰,我在最后提供了一些屏幕截图,以消除可能出现的任何歧义和误解。

在此处输入图像描述在此处输入图像描述在此处输入图像描述

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

于 2013-02-10T08:35:22.867 回答
0

您可以使用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 应用程序。

于 2014-02-18T11:22:05.297 回答