iOS开发- 获取本地视频文件
下面具体介绍下实现过程。
先看效果图。
图1. 未实现功能前, iTunes截图
图2. 实现功能后, iTunes截图
图3. 实现功能后, 运行截图
好了, 通过图片, 我们可以看到实现的效果。
功能包括: 允许通过iTunes导入文件。 可以查看沙盒下所有文件。
实现过程:
1。在应用程序的Info.plist文件中添加UIFileSharingEnabled键,并将键值设置为YES。
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
iOS开发- 获取本地视频文件的更多相关文章
- iOS - 获取音视频文件的Metadata信息
// // MusicInfoArray.h // LocationMusic // // Created by Wengrp on 2017/6/22. // Copyright © 2017年 W ...
- iOS 直播-获取音频(视频)数据
iOS 直播-获取音频(视频)数据 // // ViewController.m // capture-test // // Created by caoxu on 16/6/3. // Copyri ...
- 【转】ios开发证书,描述文件,bundle ID的关系
ios开发证书,描述文件,bundle ID的关系 苹果为了控制应用的开发与发布流程,制定了一套非常复杂的机制.这里面的关键词有:个人开发者账号,企业开发者账号,bundle ID,开发证书,发布 ...
- 获取音视频文件AVMetadata数据
获取音视频文件AVMetadata数据 问题来源: http://stackoverflow.com/questions/16318821/extracting-mp3-album-artwork-i ...
- 如何使用iOS 开发证书 和 Profile 文件
如果你想在 iOS 设备(iPhone/iPad/iTouch)上调试, 需要有 iOS 开发证书和 Profile 文件. 在你拿到这两个文件之后,该如何使用呢? 证书使用说明: 1. iOS 开 ...
- Supermap/Cesium 开发心得----本地视频接入播放
在三维中,为了增加现实感.给人一种带入感,我们会采取接入视频的方式来实现,那么如何接入视频呢? 由于没有截至写文章为止,我没有视频流数据,所以只能采取本地视频文件的方式来做. 本文介绍结束视频的其中一 ...
- Android开发之获取本地视频和获取自拍视频
1.获取本地所有视频 public void getLoadMedia() { Cursor cursor = UILApplication.instance.getApplicationContex ...
- IOS开发--数据持久化篇文件存储(二)
前言:个人觉得开发人员最大的悲哀莫过于懂得使用却不明白其中的原理.在代码之前我觉得还是有必要简单阐述下相关的一些知识点. 因为文章或深或浅总有适合的人群.若有朋友发现了其中不正确的观点还望多多指出,不 ...
- ios 开发之本地推送
网络推送可能被人最为重视,但是本地推送有时候项目中也会运用到: 闲话少叙,代码如下: 1.添加根视图 self.window.rootViewController = [[UINavigationCo ...
随机推荐
- wex5新增数据库
首先是要打开Wex5 (这是废话,下面进入正题..) 1.第一步,找到界面中的 ”窗口” 点击打开,你会看到一个 “ 首选项 ”按照流程也要打开 (囧),,,,,,,,看图为重 2.当你打开了 “ ...
- bootstrap dialog对话框,完成操作提示框
1. 依赖文件: bootstrap.js bootstrap-dialog.js bootstrap.css bootstrap-dialog.css 2.代码 BootstrapDialog.co ...
- 了解WaitForSingleObject中WAIT_ABANDONED 返回值
1.互斥量内核对象 互斥量内核对象用来确保一个线程独占对一个资源的访问.互斥量对象包含一个使用计数.线程ID以及递归计数.互斥量与关键段的行为完全相同.但是互斥量是内核对象,而关键段是用户模式下的同步 ...
- struts2返回结果类型
在action下还有result标签 1.result不只有name,其实还有type result返回类型在struts-default.xml默认的配置文件中有定义,可以看到有result-typ ...
- iOS中使用RNCryptor对资源文件加密
原文:http://blog.csdn.net/chenpolu/article/details/46277587 RNCryptor源码https://github.com/RNCryptor/RN ...
- 《ArcGIS Runtime SDK for Android开发笔记》——问题集:如何解决ArcGIS Runtime SDK for Android中文标注无法显示的问题(转载)
Geodatabase中中文标注编码乱码一直是一个比较头疼的问题之前也不知道问题出在哪里?在百度后发现园子里的zssai已经对这个问题原因做了一个详细说明.这里将原文引用如下: 说明:此文转载自htt ...
- Azure 9 月新发布
亲爱的小伙伴们, 我们很高兴向您宣布以下新功能与相关调整,欢迎关注与使用. 1. SQL 数据库弹性池 2. 存储指标更新 3. SQL 数据库 P15 4. Azure 高级存储 5. Wosig ...
- visual studio 调试 不进断点 断点失效 提示当前不会命中该断点等问题解决
1.首先看一下 当前调试模式是否为debug 2. 点击[调试]>[选项和设置] 将[要求源文件与原始文件完全匹配]勾选掉 3.点击调试的最后一个选项 点击[web] 将调试器内部勾选上需要测 ...
- JAVA利用poi获取world文件内容
本文主要简单介绍了利用poi包,读取world文件内容. 这个依然存在版本的问题,只能读取doc结尾的老版本文件. 话不多说,上代码: import java.io.File; import java ...
- python+requests+json 接口测试思路示例
实际项目中用python脚本实现接口测试的步骤: 1 发送请求,获取响应 >>2 提取响应里的数据,对数据进行必要的处理 >>3 断言响应数据是否与预期一致 以豆瓣接口为例 ...