调用相机、相册

没什么好说的,注意一点,就是调用相机的时候需要真机测试,用模拟器的时候要加一个判断语句,不然崩溃

 1 #import "RootViewController.h"
 2 #import "RootView.h"
 3
 4 @interface RootViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
 5
 6 @property (nonatomic, strong) RootView *rootView;
 7 @end
 8
 9 @implementation RootViewController
10
11 - (void)loadView {
12     self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
13     self.view = self.rootView;
14 }
15
16
17
18 - (void)viewDidLoad {
19     [super viewDidLoad];
20     // Do any additional setup after loading the view.
21
22
23     // 添加点击事件
24     [self.rootView.cameraButton addTarget:self action:@selector(cameraButtonClick:) forControlEvents:UIControlEventTouchUpInside];
25
26     [self.rootView.photoButton addTarget:self action:@selector(photoButtonClick:) forControlEvents:UIControlEventTouchUpInside];
27 }
28
29
30
31 #pragma mark - 实现点击事件
32 // 调用相机
33 - (void)cameraButtonClick:(UIButton *)sender {
34
35     // 检测可用源
36     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
37         // 1.创建对象并初始化
38         UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
39
40         // 2.添加数据来源
41         imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
42
43         // 3.设置代理
44         imgPicker.delegate = self;
45
46         // 4.是否允许编译
47         imgPicker.allowsEditing = YES;
48
49         // 5.实现页面跳转
50         [self presentViewController:imgPicker animated:YES completion:nil];
51     } else {
52         [sender setTitle:@"NO" forState:UIControlStateNormal];
53     }
54 }
55
56 // 实现代理方法
57 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
58
59     [self dismissViewControllerAnimated:YES completion:nil];
60 }
61
62
63
64 // 调用相册
65 - (void)photoButtonClick:(UIButton *)sender {
66
67     // 1.创建对象并初始化
68     UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
69
70     // 2.添加数据来源
71     imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
72
73     // 3.设置代理
74     imgPicker.delegate = self;
75
76     // 4.是否允许编译
77     imgPicker.allowsEditing = YES;
78
79     // 5.实现页面跳转
80     [self presentViewController:imgPicker animated:YES completion:nil];
81 }
82
83 @end
时间: 2024-08-27 08:42:22

调用相机、相册的相关文章

多媒体之调用相机相册及视频播放

1 #import "RootViewController.h" 2 #import <MediaPlayer/MediaPlayer.h> 3 #import <MobileCoreServices/MobileCoreServices.h> //存放了系统调用的常量 4 @interface RootViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDeleg

IOS调用相机相册

#import "SendViewController.h"  //只能打开,没有加载图片的代码,老代码,供参考 #import <MobileCoreServices/UTCoreTypes.h> @interface SendViewController ()<UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate> -(IBAction)sel

iOS调用相机,相册,上传头像

一.新建工程 二.拖控件,创建映射 三.在.h中加入delegate @interface ViewController : UIViewController 复制代码 四.实现按钮事件 -(IBAction)chooseImage:(id)sender { UIActionSheet *sheet; // 判断是否支持相机 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCam

IOS调用相机和相册时无法显示中文

调用系统相册.相机发现是英文的系统相簿界面后标题显示“photos”,但是手机语言已经设置显示中文 需要在info.plist做如下设置 info.plist里面添加 Localizedresources can be mixed YES Localization native development region China 表示是否允许应用程序获取框架库内语言.

Android图片系列(1)-------调用系统相册与相机获取图片

Android开发过程中,我们经常需要获取图片,你可以通过获取手机相册的图片,也可以调用相机拍照获取图片.这里主要讲这两个获取图片的方式,并记录其中遇到的小问题. 调用相册获取图片 这个功能非常简单,这里不多说了,这里贴出关键代码 Intent openAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT); openAlbumIntent.setType("image/*"); startActivityForResult(openAl

Android 调用系统相机拍照保存以及调用系统相册的方法

系统已经有的东西,如果我们没有新的需求的话,直接调用是最直接的.下面讲讲调用系统相机拍照并保存图片和如何调用系统相册的方法. 首先看看调用系统相机的核心方法: Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(camera, CAMERA); 相机返回的数据通过下面的回调方法取得,并处理: public static final int CAMERA  = 0x01; @Over

android 调用系统相机获取图片、调用系统相册获取图片,并对图片进行截取

打开系统相册获取图片并截取,代码相对简单 1 Intent intent = new Intent(Intent.ACTION_GET_CONTENT,null); 2 intent.setType("image/*"); 3 intent.putExtra("crop", "true"); 4 5 //WIDTH 和 HEIGHT指的是截取框的宽高比例,如设WIDTH = 1,HEIGHT = 1,截取框就为正方形 6 intent.putEx

调用系统相册或相机工具类

需求:点击修改头像,弹出对话框提示选择相册还是相机,从而调用系统相册或相机 import android.app.Activity; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import

从相册获取图片及调用相机拍照获取图片,最后上传图片到服务器

调用相机拍照获取图片: 跳转到到拍照界面: Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //下面这句指定调用相机拍照后的照片存储的路径 mSzImageFileName = Long.toString(System.currentTimeMillis()) + ".png"; takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new F

Swift—调用系统相册和相机

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #000000; min-height: 28.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px Menlo; color: #294c50 }