2013-02-01 90 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上的图片图片... –