1

我正在尝试在 pinterest 上发布我的应用程序中的图像。从过去五天开始,我在 SO 上发布了一些问题后终于寻找完美的解决方案,现在我离解决我的问题只有一步之遥了。问题是当我使用 url 发布任何图像时成功地将我的图片发布到 pinterest 上,如下面的代码所示。

-(NSString*) generatePinterestHTML {
NSString *description = @"Post your description here";
NSString *sUrl = [NSString stringWithFormat:@"http://4.bp.blogspot.com/-w4oTZjlpgwo/T5_pi-KJPuI/AAAAAAAAAoM/rKm3E0XCbgY/s1600/flower.png"];
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>"];
return htmlString;
}
-(void)viewDidLoad
{
 [super viewDidLoad];
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];
}

下面的屏幕截图显示了我上面的代码结果

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

但是当我试图从我的应用程序上传图像时,它不能正常工作这是我的代码

 - (void)viewDidLoad
 {
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
savedImagePath = [[documentsDirectory stringByAppendingPathComponent:@"savedImage.png"] mutableCopy];
NSLog(@"savedImagePath..%@",savedImagePath);
NSData*  imageData = UIImagePNGRepresentation(mypimage);
[imageData writeToFile:savedImagePath atomically:YES];
[super viewDidLoad];
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];
}
- (NSString*) generatePinterestHTML {
NSString *description = @"Post your description here";
NSString *stringUrl = [NSString stringWithFormat:@"%@",savedImagePath];
NSLog(@"stringUrl:%@", stringUrl);
NSURL *urlToUpload = [[NSURL alloc]initFileURLWithPath:stringUrl];
NSString* sUrl = [NSString stringWithFormat:@"%@",urlToUpload];
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>"];
return htmlString;
}

对于上面的代码,我的屏幕截图结果是 link1 link2所以请建议任何方法来解决这个问题。谢谢

4

1 回答 1

2

在服务器上发布图像并为 pinit 按钮分配适当的 URL 地址后,它对我有用。我在这里为那些最有可能在 pinterest 上发布来自应用程序的本地图像的人发布我的答案。

希望对大家有所帮助。

谢谢

于 2013-02-11T09:34:43.390 回答