我想使用iPhone Hooks将图片和视频上传到Instagram,但无法达到效果。
对于视频,我使用以下代码:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:videoFilePath completionBlock:^(NSURL *assetURL, NSError *error)
{
NSString *CFURL = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)[NSString stringWithFormat:@"%@", [assetURL absoluteString]], NULL, CFSTR("!$&'()*+,-./:;=?@_~"), kCFStringEncodingUTF8));
NSString *CFCaption = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)[NSString stringWithFormat:@"%@", strInstagramCaption], NULL, CFSTR("!$&'()*+,-./:;=?@_~"), kCFStringEncodingUTF8));
NSString *instagramString = [NSString stringWithFormat:@"instagram://library?AssetPath=%@&InstagramCaption=%@", [assetURL absoluteString], strInstagramCaption];
NSLog(@"instagramString : %@", instagramString);
NSURL *instagramURL = [NSURL URLWithString:instagramString];
NSLog(@"instagramURL : %@", instagramURL);
if ([[UIApplication sharedApplication] canOpenURL:instagramURL])
{
[[UIApplication sharedApplication] openURL:instagramURL];
}
}];
答案 0 :(得分:1)
以下是使用URI在Instagram上分享图片的代码。
@property (nonatomic, retain) UIDocumentInteractionController *dic;
CGRect rect = CGRectMake(0 ,0 , 0, 0);
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIGraphicsEndImageContext();
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/test.igo"];
NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:@"file://%@", jpgPath]];
self.dic.UTI = @"com.instagram.photo";
self.dic = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
self.dic=[UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
[self.dic presentOpenInMenuFromRect: rect inView: self.view animated: YES ];
- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {
UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL];
interactionController.delegate = interactionDelegate;
return interactionController;
}
您也可以从here下载示例项目。
答案 1 :(得分:0)
这是您将资产路径转换为有效NSUrl
的方式: -
NSURL *instagramURL = [NSURL URLWithString:[[NSString stringWithFormat:@"instagram://library?AssetPath=%@&InstagramCaption=%@",[assetURL absoluteString],caption]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];