从导航栏中的分享按钮显示UIDocumentInteractionController - iOS

时间:2014-08-03 18:00:51

标签: ios image tableview uidocumentinteraction

在我的项目中,我使用WSAssetPickerController

尽管工具栏不起作用(不是一个大问题),但一切正常。

我在视图控制器中添加了一个共享按钮,但我似乎无法调用UIDocumentInteractionController,我尝试复制我用于保存在apps文件夹中的文件的相同方法(哪个工作正常)。但这不是。

无关的“下载”页面如何运作:

NSString *fileName = [directoryContents objectAtIndex:indexPath.row];
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Downloads"];
path = [path stringByAppendingPathComponent:fileName];

documentController = [[UIDocumentInteractionController alloc] init];
documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path]];
[documentController setDelegate:self];
[documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];

如何加载图片:

#pragma mark - Fetching Code

    - (void)fetchAssets
    {
        // TODO: Listen to ALAssetsLibrary changes in order to update the library if it changes. 
        // (e.g. if user closes, opens Photos and deletes/takes a photo, we'll get out of range/other error when they come back.
        // IDEA: Perhaps the best solution, since this is a modal controller, is to close the modal controller.

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            [self.assetsGroup enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {

                if (!result || index == NSNotFound) {

                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self.tableView reloadData];
                        self.navigationItem.title = [NSString stringWithFormat:@"%@", [self.assetsGroup valueForProperty:ALAssetsGroupPropertyName]];
                    });

                    return;
                }

                WSAssetWrapper *assetWrapper = [[WSAssetWrapper alloc] initWithAsset:result];

                dispatch_async(dispatch_get_main_queue(), ^{

                    [self.fetchedAssets addObject:assetWrapper];

                });

            }];
        });

        [self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.5];
    }

我如何加载并调用按钮:

- (void)viewDidLoad
{
    self.navigationItem.title = @"Loading";

    UIBarButtonItem *shareButton = [[UIBarButtonItem alloc]
                                    initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                    target:self
                                    action:@selector(shareAction:)];
    self.navigationItem.rightBarButtonItem = shareButton;
    self.navigationItem.rightBarButtonItem.enabled = NO;


    // TableView configuration.
    self.tableView.contentInset = TABLEVIEW_INSETS;
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.allowsSelection = NO;


    // Fetch the assets.
    [self fetchAssets];
}

是否应该选择获取的资产

#pragma mark - WSAssetsTableViewCellDelegate Methods

- (BOOL)assetsTableViewCell:(WSAssetsTableViewCell *)cell shouldSelectAssetAtColumn:(NSUInteger)column
{
    BOOL shouldSelectAsset = (self.assetPickerState.selectionLimit == 0 ||
                              (self.assetPickerState.selectedCount < self.assetPickerState.selectionLimit));

    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    NSUInteger assetIndex = indexPath.row * self.assetsPerRow + column;

    WSAssetWrapper *assetWrapper = [self.fetchedAssets objectAtIndex:assetIndex];

    if ((shouldSelectAsset == NO) && (assetWrapper.isSelected == NO))
        self.assetPickerState.state = WSAssetPickerStateSelectionLimitReached;
    else
        self.assetPickerState.state = WSAssetPickerStatePickingAssets;

    return shouldSelectAsset;
}

- (void)assetsTableViewCell:(WSAssetsTableViewCell *)cell didSelectAsset:(BOOL)selected atColumn:(NSUInteger)column
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    // Calculate the index of the corresponding asset.
    NSUInteger assetIndex = indexPath.row * self.assetsPerRow + column;

    WSAssetWrapper *assetWrapper = [self.fetchedAssets objectAtIndex:assetIndex];
    assetWrapper.selected = selected;

    // Update the state object's selectedAssets.
    [self.assetPickerState changeSelectionState:selected forAsset:assetWrapper.asset];

    // Update navigation bar with selected count and limit variables 
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.assetPickerState.selectionLimit) {
            self.navigationItem.title = [NSString stringWithFormat:@"%@ (%lu/%ld)", [self.assetsGroup valueForProperty:ALAssetsGroupPropertyName], (unsigned long)self.assetPickerState.selectedCount, (long)self.assetPickerState.selectionLimit];
        }
    });

    if (self.assetPickerState.selectedCount == 0) {
        self.navigationItem.rightBarButtonItem.enabled = NO;
    }
    else {
        self.navigationItem.rightBarButtonItem.enabled = YES;
    }

}

以下所需的工作,例如我以前使用的下载代码。

-(void)shareAction:(id)sender {

        //Launch UIDocumentInteractionController for selected images

        documentController =[[UIDocumentInteractionController alloc]init];
        documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath://Code needed here??//]];
        documentController.delegate=self;
        [documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}

这样做的最佳做法是什么?

感谢。

更新8/4:

-(void)shareAction:(id)sender {

        //Launch UIDocumentInteractionController for selected images
    if (self.assetPickerState.selectedCount >= 1) {


        documentController = [[UIDocumentInteractionController alloc] init];
        documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:@"public.image"]];
        [documentController setDelegate:self];
        [documentController presentOptionsMenuFromRect:CGRectZero inView:self.view animated:YES];

    }

}
Returns: Unable to get data for URL: The operation couldn’t be completed. (Cocoa error 260.)

1 个答案:

答案 0 :(得分:0)

您的interactionControllerWithURL:似乎不是问题,但我发现如果没有可以 打开 的应用,则-presentOpenInMenuFromRect:不会显示strong>文件。

如果您的目的是分享该文件,并且通常并不意味着打开传统意义上的文件,那么代替: -presentOpenInMenuFromRect:inView:animated:
使用
-presentOptionsMenuFromRect:inView:animated:

前者是OpenInMenu,后者是OptionsMenu 如果差别很小,请查看我的related answer或直接查看Apple doc


实施例

//this seems fine
documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path]];

//do this
[documentController presentOptionsMenuFromRect:CGRectZero 
                                        inView:self.view
                                      animated:YES];

另外...... ,就在您出示documentInteractionController之前,最好指定文件UTI,以便documentInteractionController可以填充自己可以执行的适当选项&amp;所有可以处理此文件的应用程序列表:

示例:

//assuming the file is a PDF
[documentController setUTI:@"com.adobe.pdf"];

//or... same thing but a more standardized way would be
[documentController setUTI:(NSString *)kUTTypePDF];
//but for this second style you'll need to add the `MobileCoreServices` framework
//to your project bundle and specify the following in your .h or .m
//#import <MobileCoreServices/MobileCoreServices.h>

额外: Apple's Uniform Type Identifiers Reference