文档相关
2016-05-29 17:02
399 查看
1、调用相机、相册、图库
2、调用iBook
需求调试条件:(iOS4.0 later)
3、打开word、execl、pdf等文档
#import "ViewController.h" #import <MobileCoreServices/MobileCoreServices.h> #import <AVFoundation/AVFoundation.h> #import <MediaPlayer/MediaPlayer.h> @interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate> { BOOL canmerIsOK; BOOL photoLibaryIsOk; BOOL photosAlbumIsOk; UIImagePickerController *_imagePickerController; } @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation ViewController - (IBAction)showImagePickerView:(id)sender { //NSLog(@"相册"); _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:_imagePickerController animated:YES completion:nil]; } - (IBAction)useCamera:(id)sender { _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; //录制视频时长,默认10s _imagePickerController.videoMaximumDuration = 15; //相机类型(拍照、录像...)字符串需要做相应的类型转换 _imagePickerController.mediaTypes = @[(NSString *)kUTTypeMovie,(NSString *)kUTTypeImage]; //视频上传质量 //UIImagePickerControllerQualityTypeHigh高清 //UIImagePickerControllerQualityTypeMedium中等质量 //UIImagePickerControllerQualityTypeLow低质量 //UIImagePickerControllerQualityType640x480 _imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh; //设置摄像头模式(拍照,录制视频)为录像模式 _imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto; [self presentViewController:_imagePickerController animated:YES completion:nil]; } - (void)viewDidLoad { [super viewDidLoad]; _imagePickerController = [[UIImagePickerController alloc] init]; _imagePickerController.delegate = self; _imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; _imagePickerController.allowsEditing = YES; // Do any additional setup after loading the view, typically from a nib. } #pragma mark UIImagePickerControllerDelegate //该代理方法仅适用于只选取图片时 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo { NSLog(@"选择完毕----image:%@-----info:%@",image,editingInfo); } //适用获取所有媒体资源,只需判断资源类型 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType]; //判断资源类型 if ([mediaType isEqualToString:(NSString *)kUTTypeImage]){ //如果是图片 self.imageView.image = info[UIImagePickerControllerEditedImage]; //压缩图片 NSData *fileData = UIImageJPEGRepresentation(self.imageView.image, 1.0); //保存图片至相册 UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); //上传图片 // [self uploadImageWithData:fileData]; }else{ //如果是视频 NSURL *url = info[UIImagePickerControllerMediaURL]; //播放视频 // _moviePlayer.contentURL = url; // [_moviePlayer play]; //保存视频至相册(异步线程) NSString *urlStr = [url path]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlStr)) { UISaveVideoAtPathToSavedPhotosAlbum(urlStr, self, @selector(video:didFinishSavingWithError:contextInfo:), nil); } }); NSData *videoData = [NSData dataWithContentsOfURL:url]; //视频上传 // [self uploadVideoWithData:videoData]; } [self dismissViewControllerAnimated:YES completion:nil]; } #pragma mark 图片保存完毕的回调 - (void) image: (UIImage *) image didFinishSavingWithError:(NSError *) error contextInfo: (void *)contextInf{ } #pragma mark 视频保存完毕的回调 - (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInf{ if (error) { NSLog(@"保存视频过程中发生错误,错误信息:%@",error.localizedDescription); }else{ NSLog(@"视频保存成功."); } }
2、调用iBook
需求调试条件:(iOS4.0 later)
1NSString *fileToOpen = [[NSBundle mainBundle] pathForResource:@"readme" ofType:@"pdf"]; NSURL *url = [NSURL fileURLWithPath:fileToOpen]; docController = [[UIDocumentInteractionController interactionControllerWithURL:url] retain]; BOOL isValid = [docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
3、打开word、execl、pdf等文档
方法一: 用UIWebView就可以了 -(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView { NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil]; NSURL *url = [NSURL fileURLWithPath:path]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [webView loadRequest:request]; } // Calling -loadDocument:inView: [self loadDocument:@"test.doc" inView:self.myWebview]; 方法我也已经测试过了,希望对大家有帮助, 方法二: 下面方法是直接通过QLPreviewController打开文档 qlViewController = [[QLPreviewController alloc] init]; qlViewController.dataSource = self; [self presentModalViewController:qlViewController animated:YES]; - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller { return 1; } - (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{ //-------------读文件 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; if (!documentsDirectory) { NSLog(@"Documents directory not found!");//return ; } NSString *fileName=[NSString stringWithFormat:@"%@.%@",nameQ,extQ]; NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName]; //------------- NSURL *myQLDocument = [NSURL fileURLWithPath:appFile]; return myQLDocument; }
相关文章推荐
- iOS 网络篇5—数据安全
- 1053. 住房空置率 (20)
- Linux下1号进程的前世(kernel_init)今生(init进程)----Linux进程的管理与调度(六)
- __stdcall,__cdecl,_cdecl,_stdcall,__fastcall,_fastcall 区别简介 和 extern "C" 的作用
- OpenCV学习Sobel 导数
- 学习Winform分组类控件(Panel、groupBox、TabControl)
- Java动态代理
- JSP使用过滤器防止SQL注入
- TVP5150摄像头
- 树的直径
- 爬虫系列1:python简易爬虫分析
- JAVA——System.in/字符流与字节流
- 算法系列(二)查找算法--基本查找和二分查找
- 反转算法
- [译]CSS-理解百分比的background-position
- linux安装tomcat
- hdu5579 2015区域赛上海赛区G.Game of Arrays
- 学生信息管理系统----错误集锦(二)
- Jaccard相似系数和皮尔逊相关系数
- CentOS-6.5-x86 安装 jdk-7u79-linux-i586.rpm