1、调用相机、相册、图库
1 #import "ViewController.h" 2 #import <MobileCoreServices/MobileCoreServices.h> 3 #import <AVFoundation/AVFoundation.h> 4 #import <MediaPlayer/MediaPlayer.h> 5 6 7 @interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate> 8 { 9 BOOL canmerIsOK; 10 BOOL photoLibaryIsOk; 11 BOOL photosAlbumIsOk; 12 UIImagePickerController *_imagePickerController; 13 } 14 15 @property (weak, nonatomic) IBOutlet UIImageView *imageView; 16 17 @end 18 19 @implementation ViewController 20 - (IBAction)showImagePickerView:(id)sender { 21 //NSLog(@"相册"); 22 _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 23 24 [self presentViewController:_imagePickerController animated:YES completion:nil]; 25 26 } 27 - (IBAction)useCamera:(id)sender { 28 _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; 29 //录制视频时长,默认10s 30 _imagePickerController.videoMaximumDuration = 15; 31 32 //相机类型(拍照、录像...)字符串需要做相应的类型转换 33 _imagePickerController.mediaTypes = @[(NSString *)kUTTypeMovie,(NSString *)kUTTypeImage]; 34 35 //视频上传质量 36 //UIImagePickerControllerQualityTypeHigh高清 37 //UIImagePickerControllerQualityTypeMedium中等质量 38 //UIImagePickerControllerQualityTypeLow低质量 39 //UIImagePickerControllerQualityType640x480 40 _imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh; 41 42 //设置摄像头模式(拍照,录制视频)为录像模式 43 _imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto; 44 [self presentViewController:_imagePickerController animated:YES completion:nil]; 45 46 47 } 48 49 - (void)viewDidLoad { 50 [super viewDidLoad]; 51 52 _imagePickerController = [[UIImagePickerController alloc] init]; 53 _imagePickerController.delegate = self; 54 _imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 55 _imagePickerController.allowsEditing = YES; 56 57 58 59 60 // Do any additional setup after loading the view, typically from a nib. 61 } 62 63 #pragma mark UIImagePickerControllerDelegate 64 //该代理方法仅适用于只选取图片时 65 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo { 66 NSLog(@"选择完毕----image:%@-----info:%@",image,editingInfo); 67 } 68 69 70 //适用获取所有媒体资源,只需判断资源类型 71 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ 72 NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType]; 73 //判断资源类型 74 if ([mediaType isEqualToString:(NSString *)kUTTypeImage]){ 75 //如果是图片 76 self.imageView.image = info[UIImagePickerControllerEditedImage]; 77 //压缩图片 78 NSData *fileData = UIImageJPEGRepresentation(self.imageView.image, 1.0); 79 //保存图片至相册 80 UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); 81 //上传图片 82 // [self uploadImageWithData:fileData]; 83 84 }else{ 85 //如果是视频 86 NSURL *url = info[UIImagePickerControllerMediaURL]; 87 //播放视频 88 // _moviePlayer.contentURL = url; 89 // [_moviePlayer play]; 90 //保存视频至相册(异步线程) 91 NSString *urlStr = [url path]; 92 93 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 94 if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlStr)) { 95 96 UISaveVideoAtPathToSavedPhotosAlbum(urlStr, self, @selector(video:didFinishSavingWithError:contextInfo:), nil); 97 } 98 }); 99 NSData *videoData = [NSData dataWithContentsOfURL:url]; 100 //视频上传 101 // [self uploadVideoWithData:videoData]; 102 } 103 [self dismissViewControllerAnimated:YES completion:nil]; 104 } 105 106 107 #pragma mark 图片保存完毕的回调 108 - (void) image: (UIImage *) image didFinishSavingWithError:(NSError *) error contextInfo: (void *)contextInf{ 109 110 } 111 112 #pragma mark 视频保存完毕的回调 113 - (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInf{ 114 if (error) { 115 NSLog(@"保存视频过程中发生错误,错误信息:%@",error.localizedDescription); 116 }else{ 117 NSLog(@"视频保存成功."); 118 } 119 }
2、调用iBook
需求调试条件:(iOS4.0 later)
1 1NSString *fileToOpen = [[NSBundle mainBundle] pathForResource:@"readme" ofType:@"pdf"]; 2 NSURL *url = [NSURL fileURLWithPath:fileToOpen]; 3 docController = [[UIDocumentInteractionController interactionControllerWithURL:url] retain]; 4 BOOL isValid = [docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
3、打开word、execl、pdf等文档
1 方法一: 2 用UIWebView就可以了 3 -(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView 4 { 5 NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil]; 6 NSURL *url = [NSURL fileURLWithPath:path]; 7 NSURLRequest *request = [NSURLRequest requestWithURL:url]; 8 [webView loadRequest:request]; 9 } 10 11 // Calling -loadDocument:inView: 12 [self loadDocument:@"test.doc" inView:self.myWebview]; 13 14 15 方法我也已经测试过了,希望对大家有帮助, 16 17 18 方法二: 19 下面方法是直接通过QLPreviewController打开文档 20 21 qlViewController = [[QLPreviewController alloc] init]; 22 qlViewController.dataSource = self; 23 [self presentModalViewController:qlViewController animated:YES]; 24 25 26 - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller { 27 return 1; 28 } 29 - (id <QLPreviewItem>)previewController:(QLPreviewController *)controller 30 previewItemAtIndex:(NSInteger)index{ 31 //-------------读文件 32 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 33 NSString *documentsDirectory = [paths objectAtIndex:0]; 34 if (!documentsDirectory) { 35 NSLog(@"Documents directory not found!");//return ; 36 } 37 NSString *fileName=[NSString stringWithFormat:@"%@.%@",nameQ,extQ]; 38 NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName]; 39 //------------- 40 NSURL *myQLDocument = [NSURL fileURLWithPath:appFile]; 41 return myQLDocument; 42 }
时间: 2024-11-05 19:39:44