iOS開發- 獲取本地視頻文件

下面具體介紹下實現過程。
先看效果圖。
圖1. 未實現功能前, iTunes截圖
http://img.blog.csdn.net/20140608195557265?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGl0d2h5bHo=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

圖2. 實現功能后, iTunes截圖
http://img.blog.csdn.net/20140608195628703?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGl0d2h5bHo=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

圖3. 實現功能后, 運行截圖。
http://img.blog.csdn.net/20140608195653515

好了, 通過圖片, 我們可以看到實現的效果。
功能包括: 允許通過iTunes導入文件。 可以查看沙盒下所有文件。

實現過程:
1。在應用程序的Info.plist文件中添加UIFileSharingEnabled鍵,并將鍵值設置為YES。
http://img.blog.csdn.net/20140608200008812?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGl0d2h5bHo=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

2。具體代碼:
ViewController.h

import <UIKit/UIKit.h>

//step1. 導入QuickLook庫和頭文件

import <QuickLook/QuickLook.h>

//step2. 繼承協議
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,QLPreviewControllerDataSource,QLPreviewControllerDelegate,UIDocumentInteractionControllerDelegate>
{
//step3. 聲明顯示列表
IBOutlet UITableView *readTable;
}

//setp4. 聲明變量
//UIDocumentInteractionController : 一個文件交互控制器,提供應用程序管理與本地系統中的文件的用戶交互的支持
//dirArray : 存儲沙盒子里面的所有文件
@property(nonatomic,retain) NSMutableArray *dirArray;
@property (nonatomic, strong) UIDocumentInteractionController *docInteractionController;
@end

ViewController.m

  • (void)viewDidLoad
    {
    [super viewDidLoad];
//step5. 保存一張圖片到設備document文件夾中(為了測試方便)  
UIImage *image = [UIImage imageNamed:@"testPic.jpg"];  
NSData *jpgData = UIImageJPEGRepresentation(image, 0.8);  
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory  
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"testPic.jpg"]; //Add the file name  
[jpgData writeToFile:filePath atomically:YES]; //Write the file  
  
  
//step5. 保存一份txt文件到設備document文件夾中(為了測試方便)  
char *saves = "Colin_csdn";  
NSData *data = [[NSData alloc] initWithBytes:saves length:10];  
filePath = [documentsPath stringByAppendingPathComponent:@"colin.txt"];  
[data writeToFile:filePath atomically:YES];  
  
  
//step6. 獲取沙盒里所有文件  
NSFileManager *fileManager = [NSFileManager defaultManager];  
//在這里獲取應用程序Documents文件夾里的文件及文件夾列表  
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSString *documentDir = [documentPaths objectAtIndex:0];  
NSError *error = nil;  
NSArray *fileList = [[NSArray alloc] init];  
//fileList便是包含有該文件夾下所有文件的文件名及文件夾名的數組  
fileList = [fileManager contentsOfDirectoryAtPath:documentDir error:&error];  
  
self.dirArray = [[NSMutableArray alloc] init];  
for (NSString *file in fileList)  
{  
    [self.dirArray addObject:file];  
}  

//step6. 刷新列表, 顯示數據
[readTable reloadData];
}

//step7. 利用url路徑打開UIDocumentInteractionController

  • (void)setupDocumentControllerWithURL:(NSURL *)url
    {
    if (self.docInteractionController == nil)
    {
    self.docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];
    self.docInteractionController.delegate = self;
    }
    else
    {
    self.docInteractionController.URL = url;
    }
    }

pragma mark- 列表操作

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    return 1;
    }
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellName = @"CellName";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellName];
    if (cell == nil)
    {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellName];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    NSURL *fileURL= nil;
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir = [documentPaths objectAtIndex:0];
    NSString *path = [documentDir stringByAppendingPathComponent:[self.dirArray objectAtIndex:indexPath.row]];
    fileURL = [NSURL fileURLWithPath:path];
    [self setupDocumentControllerWithURL:fileURL];
    cell.textLabel.text = [self.dirArray objectAtIndex:indexPath.row];
    NSInteger iconCount = [self.docInteractionController.icons count];
    if (iconCount > 0)
    {
    cell.imageView.image = [self.docInteractionController.icons objectAtIndex:iconCount - 1];
    }

    return cell;
    }

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return [self.dirArray count];
    }

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    QLPreviewController *previewController = [[QLPreviewController alloc] init];
    previewController.dataSource = self;
    previewController.delegate = self;

    // start previewing the document at the current section index
    previewController.currentPreviewItemIndex = indexPath.row;
    [[self navigationController] pushViewController:previewController animated:YES];
    // [self presentViewController:previewController animated:YES completion:nil];
    }

pragma mark - UIDocumentInteractionControllerDelegate

  • (NSString *)applicationDocumentsDirectory
    {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    }

  • (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)interactionController
    {
    return self;
    }

pragma mark - QLPreviewControllerDataSource

// Returns the number of items that the preview controller should preview

  • (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController
    {
    return 1;
    }

  • (void)previewControllerDidDismiss:(QLPreviewController *)controller
    {
    // if the preview dismissed (done button touched), use this method to post-process previews
    }

// returns the item that the preview controller should preview

  • (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)idx
    {
    NSURL *fileURL = nil;
    NSIndexPath *selectedIndexPath = [readTable indexPathForSelectedRow];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir = [documentPaths objectAtIndex:0];
    NSString *path = [documentDir stringByAppendingPathComponent:[self.dirArray objectAtIndex:selectedIndexPath.row]];
    fileURL = [NSURL fileURLWithPath:path];
    return fileURL;
    }
  • (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

@end

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容