1.

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:                               @"设置头像" delegate:self cancelButtonTitle:@"取消"                       destructiveButtonTitle:nil otherButtonTitles:@"选择本地图片",@"拍照", nil];

[actionSheet showInView:self.view];

//2.实现相应代理事件,代理UIActionSheetDelegate,方法如下

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:                                                       (NSInteger)buttonIndex {

// 相册 0 拍照 1

switch (buttonIndex) {

case 0:

//从相册中读取

[self readImageFromAlbum];

break;

case 1:

//拍照

[self readImageFromCamera];

break;

default:

break;

}

}

//3.实现从相册读取图片功能,代码如下

//从相册中读取

- (void)readImageFromAlbum {

//创建对象

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

//(选择类型)表示仅仅从相册中选取照片

imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

//指定代理,因此我们要实现UIImagePickerControllerDelegate,                                                 UINavigationControllerDelegate协议

imagePicker.delegate = self;

//设置在相册选完照片后,是否跳到编辑模式进行图片剪裁。(允许用户编辑)

imagePicker.allowsEditing = YES;

//显示相册

[self presentViewController:imagePicker animated:YES completion:nil];

}

//4.实现拍照功能

- (void)readImageFromCamera {

if ([UIImagePickerController isSourceTypeAvailable:                                           UIImagePickerControllerSourceTypeCamera]) {

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];         imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;         imagePicker.delegate = self;

imagePicker.allowsEditing = YES;

//允许用户编辑

[self presentViewController:imagePicker animated:YES completion:nil];

} else {

//弹出窗口响应点击事件

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告"                         message:@"未检测到摄像头" delegate:nil cancelButtonTitle:nil                                                 otherButtonTitles:@"确定", nil];

[alert show];

}

}

//5.图片完成处理后提交,代理方法UIPickerControllerDelegate

//图片完成之后处理

- (void)imagePickerController:(UIImagePickerController *)picker        didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {

//image 就是修改后的照片

//将图片添加到对应的视图上

[_headImageView setImage:image];

//结束操作

[self dismissViewControllerAnimated:YES completion:nil];

}

最新文章

  1. C# 本质论 第二章 数据类型
  2. XObject.java 对象还没写完,希望电脑不会丢失。坏笑,早点见。
  3. MapReduce应用案例--简单的数据去重
  4. 网易新闻iOS版使用的18个开源组件
  5. linux下改变文件的字符编码
  6. NOIP2011 铺地毯
  7. 为什么 API 监控对于任何业务来说都重要?
  8. Python传参数最简单易懂的描述
  9. TWinControl.DefaultHandler处理WM_CTLCOLORMSGBOX..WM_CTLCOLORSTATIC消息的两个参数很有意思,两个都是传递句柄
  10. hadoop(六) - ZooKeeper安装与配置
  11. Python读取ini配置文件
  12. Java 泛型 协变式覆盖和泛型重载
  13. Django在form提交CSRF验证失败. 相应中断问题
  14. Android高级控件(四)——VideoView 实现引导页播放视频欢迎效果,超级简单却十分的炫酷
  15. zoj3469 区间dp好题
  16. python接口自动化28-requests-html爬虫框架
  17. 白鹭引擎 - 矢量绘图 ( graphics )
  18. angularjs之事件绑定、解除事件绑定
  19. Mysql双向同步热备份设置
  20. Delphi Cookie获取及使用

热门文章

  1. Vue.js学习笔记(4)
  2. CSS3 Media Queries实现响应式布局
  3. 关于javascript中限定时间内防止按钮重复点击的思路
  4. 掌握 cinder-scheduler 调度逻辑 - 每天5分钟玩转 OpenStack(48)
  5. Android启动icon切图大小
  6. [Python] python vs cplusplus
  7. Global eval. What are the options?
  8. T-SQL CROSS APPLY、MERGE
  9. Elasticsearch+Logstash+Kibana教程
  10. RECONFIGURE语句会清空计划缓存么?