我正在尝试在 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;
}