通过另一个应用iOS将视频上传到Instagram

时间:2014-06-20 06:30:27

标签: ios iphone objective-c instagram

我正在尝试将从我的应用程序捕获的视频分享到Instagram但应用程序崩溃。照片成功分享。

使用的代码:

     NSData* videoData = [NSData dataWithContentsOfURL:info[@"UIImagePickerControllerMediaURL"]];
     NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"instagram.igo"];
     [videoData writeToFile:path atomically:YES];
      NSURL *fileURL=[NSURL fileURLWithPath:path];

 NSDictionary *fileCaption = @{ @"InstagramCaption" : @"Shared by my app" };

     [self setDocumentInteractionController:[self setupControllerWithURL:fileURL usingDelegate:self]];
            [self.documentInteractionController setUTI:@"com.instagram.exclusivegram"];
            [self.documentInteractionController setAnnotation:fileCaption];
            [self.documentInteractionController presentOpenInMenuFromRect:rect inView:self.view animated:YES];

在捕获视频时,我的应用会重定向并打开Instagram应用,但会立即崩溃。

有人可以帮助我解决我的错误或告诉我正确的方法来分享Instagram的视频吗?

2 个答案:

答案 0 :(得分:0)

我怀疑你在哪里编写视频进行分享。在我的应用程序中,我使用以下内容生成一个位置:

    NSString *cachesFolder = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *fileName = [cachesFolder stringByAppendingPathComponent:@"journal.pdf"];
    ... write the file ... 

然后我就像这样分享:

    _documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:fileName]];
    _documentController.delegate = self;
    [_documentController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];

答案 1 :(得分:0)

我做到了,我必须将视频保存到设备库,并在Instagram打开时打开相机,然后用户可以从他的视频库中选择他想要的任何视频..

我使用了这段代码:

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"instagram://location?id=1"]]) {

    NSURL *instagramURL = [NSURL URLWithString:@"instagram://camera"];

    NSString  *instagramIgo = [NSTemporaryDirectory() stringByAppendingPathComponent:@"instagram.igo"];
    NSLog(@"video path == %@",instagramIgo);

    // Save video to library ---

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    // don't forget to link to the AssetsLibrary framework
    // and also #import <AssetsLibrary/AssetsLibrary.h>

    NSString *filePathString = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"mp4"];
    NSURL *filePathURL = [NSURL fileURLWithPath:filePathString isDirectory:NO];
    if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:filePathURL]) {
        [library writeVideoAtPathToSavedPhotosAlbum:filePathURL completionBlock:^(NSURL *assetURL, NSError *error){
            if (error) {
                // TODO: error handling
            } else {
                // TODO: success handling
            }
        }];
    }
    [library release];


    CGRect rect = CGRectMake(0 ,0 , 0, 0);
    NSURL *fileURL = [NSURL fileURLWithPath:instagramIgo];

    NSLog(@"---- %@",fileURL);

    self.docController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
    self.docController.delegate = self;
    [self.docController setUTI:@"com.instagram.exclusivegram"];
    [self.docController setAnnotation:@{@"InstagramCaption" : @"Cloud Epic Mar"}];
    [ self.docController presentOpenInMenuFromRect:rect inView:appDelegate.window.rootViewController.view animated:YES];

    [[UIApplication sharedApplication] openURL:instagramURL];
}

我希望能帮到你

编辑: 另外,在保存之前,您应该检查视频是否在库中。