上传文件时,我们都的从本地中选择或用相机来拍摄得到文件。

一个上传按钮,单击事件

 1 -(IBAction)btnClick{
2 UIActionSheet* actionSheet = [[UIActionSheet alloc]
3 initWithTitle:@"请选择文件来源"
4 delegate:self
5 cancelButtonTitle:@"取消"
6 destructiveButtonTitle:nil
7 otherButtonTitles:@"照相机",@"摄像机",@"本地相簿",@"本地视频",nil];
8 [actionSheet showInView:self.view];
9 [actionSheet release];
10 }

点击按钮触发的btnClick事件后将会弹出一个如下的选择筐:

接下来将要为UIActionSheet实现其中的委托了。

 1 #pragma mark -
2 #pragma UIActionSheet Delegate
3 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
4 {
5 NSLog(@"buttonIndex = [%d]",buttonIndex);
6 switch (buttonIndex) {
7 case 0://照相机
8 {10 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
11 imagePicker.delegate = self;
12 imagePicker.allowsEditing = YES;
13 imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
14 imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];
15 [self presentModalViewController:imagePicker animated:YES];
16 [imagePicker release];
17 }
18 break;
19 case 1://摄像机
20 {22 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
23 imagePicker.delegate = self;
24 imagePicker.allowsEditing = YES;
25 imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
26 imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
27 imagePicker.videoQuality = UIImagePickerControllerQualityTypeLow;
28 [self presentModalViewController:imagePicker animated:YES];
29 [imagePicker release];
30 }
31 break;
32 case 2://本地相簿
33 {35 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
36 imagePicker.delegate = self;
37 imagePicker.allowsEditing = YES;
38 imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
39 imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];
40 [self presentModalViewController:imagePicker animated:YES];
41 [imagePicker release];
42 }
43 break;
44 case 3://本地视频
45 {47 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
48 imagePicker.delegate = self;
49 imagePicker.allowsEditing = YES;
50 imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
51 imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
52 [self presentModalViewController:imagePicker animated:YES];
53 [imagePicker release];
54 }
55 break;
56 default:
57 break;
58 }
59 }

实现了UIActionSheet的委托后,要发现,我们使用了UIImagePickerController,这个类将帮我们实现选取文件,打开对应的选取方式。比如当ButtonIndex为0的时候,它将帮我们打开照相机,我们可以使用相机拍摄照片作为上传的选取文件。因此,在这里我们还要实现UIImagePickerController的委托:

 1 #pragma mark -
2 #pragma UIImagePickerController Delegate
3 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
4 {
5 if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeImage]) {
6 UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
7 self.fileData = UIImageJPEGRepresentation(img, 1.0);
8 } else if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeMovie]) {
9 NSString *videoPath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
10 self.fileData = [NSData dataWithContentsOfFile:videoPath];
11 }
12 [picker dismissModalViewControllerAnimated:YES];
13 }
14
15 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
16 {
17 [picker dismissModalViewControllerAnimated:YES];
18 }

之后,你选取的文件便保存在了filedata中。就可以随时过来调用了。

IOS获取摄像和本地中的资源的更多相关文章

  1. iOS获取ipa素材、提取ipa资源图片文件

    当我们看到一款优秀的App时,我们可能对它的一些素材比较感兴趣,或者我们也想仿写一款类似app,那么怎么能获取到它的素材资源文件呢? 下面我以ofo举例: 1.打开iTunes,搜索ofo关键字,选择 ...

  2. iOS获取网络数据/路径中的文件名

    NSString * urlString = @"http://www.baidu.com/img/baidu_logo_fqj_10.gif"; //方法一:最直接 NSStri ...

  3. iOS学习——如何在mac上获取开发使用的模拟器的资源以及模拟器中每个应用的应用沙盒

    如题,本文主要研究如何在mac上获取开发使用的模拟器的资源以及模拟器中每个应用的应用沙盒.做过安卓开发的小伙伴肯定很方便就能像打开资源管理器一样查看我们写到手机本地或应用中的各种资源,但是在iOS开发 ...

  4. iOS WebView 加载本地资源(图片,文件等)

    https://www.cnblogs.com/dhui69/p/5596917.html iOS WebView 加载本地资源(图片,文件等) NSString *path = [[NSBundle ...

  5. Pyqt 获取打包二进制文件中的资源

    记得有一次打开一个单独exe程序,点击btn中的一个帮助说明按钮,在同级目录下就多出一个help.chm 文件并自动打开. 那这个exe肯定是把help.chm 打包到exe中,当我触发“帮助”按钮的 ...

  6. IOS获取物理尺寸中7Plus中获取的是7的物理尺寸

    IOS获取物理尺寸中7Plus中获取的是7的物理尺寸: 在开发调试过程中我的7Plus手机获取[uiscreen mainscreen].bounds为750  .1334. 解决方案:在手机中的显示 ...

  7. 修正iOS从照相机和相册中获取的图片 方向

    修正iOS从照相机和相册中获取的图片 方向   修正iOS从照相机和相册中获取的图片 方向 使用系统相机拍照得到的图片的默认方向有时不是ImageOrientationDown,而是ImageOrie ...

  8. Java如何获取当前的jar包路径以及如何读取jar包中的资源

    写作业的时候要输出一个record.dat文件到jar包的同级目录,但是不知道怎么定位jar包的路径.百度到的方法不很靠谱,所以在这里记录一下. 一:使用类路径 String path = this. ...

  9. springmvc获取jar中的静态资源与jar包中的资源互相引用问题

    1.首先看jar中的文件位置 2.在web工程中引用该jar 并且在springmvc文件中配置路径 如果有多个路径可用逗号隔开 3.在web工程找jsp页面如何引用 这样就可以了 关于jar中的资源 ...

随机推荐

  1. marmalade android 5.0 JNI 调用失败的解决方案

    5.0 真机日志如下:sart/runtime/check_jni.cc:65] JNI DETECTED ERROR IN APPLICATION: JNI CallVoidMethodV call ...

  2. [itint5]棋盘漫步

    要注意dp[0][0]要初始化为1. int totalPath(vector<vector<bool> > &blocked) { int m = blocked.s ...

  3. UIBezierPath画圆弧的记录

    UIBezierPath通过 - (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)s ...

  4. HashMap源代码深入剖析

    ..

  5. HDU1171——Big Event in HDU(母函数)

    Big Event in HDU DescriptionNowadays, we all know that Computer College is the biggest department in ...

  6. python 操作符笔记:

    操作符 描述 x if y else z 三元描述(2.5新加)(类似于c中的 x?y:z x or y 或 x and y 与 not x 非 x!=y ,x is y 序列成员测试 x | y 位 ...

  7. ARMv7 .n和.w指令宽度指示符后缀

    Thumb code里,.n后缀强迫生成16bit的代码,即Thumb code,若是在arm code里用.n会报错,若是机器指令没有办法用16表示也会报错 Thumb code里,.w后缀强迫生成 ...

  8. Linux Shell常用命令手册(Updating)

    检查远程端口是否对bash开放: nc -nvv $IP $PORT telnet $IP $PORT 当前任务的前后台切换: Ctrl + z fg 截取变量前5个字符: ${variable:0: ...

  9. freemarker 如何获得list的索引值

    <#list toplist as toplists> ${toplists_index} </#list> 相当方便

  10. BZOJ2500: 幸福的道路

    题解: 一道不错的题目. 树DP可以求出从每个点出发的最长链,复杂度O(n) 然后就变成找一个数列里最长的连续区间使得最大值-最小值<=m了. 成了这题:http://www.cnblogs.c ...