使用 iPhone Hooks 将照片中的图像/视频上传到 Instagram
Posted
技术标签:
【中文标题】使用 iPhone Hooks 将照片中的图像/视频上传到 Instagram【英文标题】:Upload Image/Video from Photos to Instagram using iPhone Hooks 【发布时间】:2015-06-01 07:40:08 【问题描述】:我想使用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];
];
【问题讨论】:
那你遇到了什么问题? Create NSURL using AssetPath的可能重复 此代码不起作用 【参考方案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下载示例项目。
【讨论】:
【参考方案2】:这就是你应该如何将资产路径转换为有效的NSUrl
:-
NSURL *instagramURL = [NSURL URLWithString:[[NSString stringWithFormat:@"instagram://library?AssetPath=%@&InstagramCaption=%@",[assetURL absoluteString],caption]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
【讨论】:
以上是关于使用 iPhone Hooks 将照片中的图像/视频上传到 Instagram的主要内容,如果未能解决你的问题,请参考以下文章
将表示图像的 NSMutableData 保存到 iPhone 照片库