iOS 调用相机拍照和选择图库图片 设置头像
2015-07-01 11:05
841 查看
不多说,直接上代码
#import "ViewController.h"
@interface
ViewController ()<UIImagePickerControllerDelegate,UIActionSheetDelegate>
@property(nonatomic,strong)UIButton *btn;
@property(nonatomic,strong)UIActionSheet *actionSheet;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_btn = [UIButton
buttonWithType:UIButtonTypeSystem];
_btn.frame =
CGRectMake(80,
200, 200,
200);
_btn.backgroundColor = [UIColor
yellowColor];
[_btn
addTarget:self
action:@selector(btnClick:)
forControlEvents:UIControlEventTouchUpInside];
[_btn
setTitle:@"点我"
forState:UIControlStateNormal];
[self.view
addSubview:_btn];
}
- (void)btnClick:(UIButton *)sender
{
[self
openActionSheetFunc];
}
//调用ActionSheet
- (void)openActionSheetFunc
{
//判断设备是否有具有摄像头(相机)功能
if ([UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
_actionSheet = [[UIActionSheet
alloc]initWithTitle:@"选择图像"
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"拍照",@"从相册选择",
nil];
}
else
{
_actionSheet = [[UIActionSheet
alloc]initWithTitle:@"选择图像"
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"从相册选择",
nil];
}
_actionSheet.tag =
100;
//显示提示栏
[_actionSheet
showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (actionSheet.tag ==
100)
{
NSUInteger sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
if ([UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
switch (buttonIndex)
{
case
0:
//来源:相机
sourceType =
UIImagePickerControllerSourceTypeCamera;
break;
case
1:
//来源:相册
sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
break;
case
2:
return;
}
}
else
{
if (buttonIndex ==
2)
{
return;
}
else
{
sourceType =
UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
}
//跳转到相机或者相册页面
UIImagePickerController *imagePickerController = [[UIImagePickerController
alloc]init];
imagePickerController.allowsEditing =
YES;
imagePickerController.sourceType = sourceType;
imagePickerController.delegate =
self;
[self
presentViewController:imagePickerController
animated:YES
completion:nil];
}
}
//pickerController的代理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary
*)info
{
[picker dismissViewControllerAnimated:YES
completion:nil];
UIImage *image = [info
objectForKey:UIImagePickerControllerOriginalImage];
[_btn
setBackgroundImage:image forState:UIControlStateNormal];
}
a934
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import "ViewController.h"
@interface
ViewController ()<UIImagePickerControllerDelegate,UIActionSheetDelegate>
@property(nonatomic,strong)UIButton *btn;
@property(nonatomic,strong)UIActionSheet *actionSheet;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_btn = [UIButton
buttonWithType:UIButtonTypeSystem];
_btn.frame =
CGRectMake(80,
200, 200,
200);
_btn.backgroundColor = [UIColor
yellowColor];
[_btn
addTarget:self
action:@selector(btnClick:)
forControlEvents:UIControlEventTouchUpInside];
[_btn
setTitle:@"点我"
forState:UIControlStateNormal];
[self.view
addSubview:_btn];
}
- (void)btnClick:(UIButton *)sender
{
[self
openActionSheetFunc];
}
//调用ActionSheet
- (void)openActionSheetFunc
{
//判断设备是否有具有摄像头(相机)功能
if ([UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
_actionSheet = [[UIActionSheet
alloc]initWithTitle:@"选择图像"
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"拍照",@"从相册选择",
nil];
}
else
{
_actionSheet = [[UIActionSheet
alloc]initWithTitle:@"选择图像"
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"从相册选择",
nil];
}
_actionSheet.tag =
100;
//显示提示栏
[_actionSheet
showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (actionSheet.tag ==
100)
{
NSUInteger sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
if ([UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
switch (buttonIndex)
{
case
0:
//来源:相机
sourceType =
UIImagePickerControllerSourceTypeCamera;
break;
case
1:
//来源:相册
sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
break;
case
2:
return;
}
}
else
{
if (buttonIndex ==
2)
{
return;
}
else
{
sourceType =
UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
}
//跳转到相机或者相册页面
UIImagePickerController *imagePickerController = [[UIImagePickerController
alloc]init];
imagePickerController.allowsEditing =
YES;
imagePickerController.sourceType = sourceType;
imagePickerController.delegate =
self;
[self
presentViewController:imagePickerController
animated:YES
completion:nil];
}
}
//pickerController的代理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary
*)info
{
[picker dismissViewControllerAnimated:YES
completion:nil];
UIImage *image = [info
objectForKey:UIImagePickerControllerOriginalImage];
[_btn
setBackgroundImage:image forState:UIControlStateNormal];
}
a934
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- 按右键另存图片只能存BMP
- photoshop去除图片上的水印
- 图片引发的溢出危机(图)
- 修复mysql数据库
- C#实现把彩色图片灰度化代码分享
- C#监控文件夹并自动给图片文件打水印的方法
- 纯CSS实现的当鼠标移上图片添加阴影效果代码
- 随鼠标移动的图片或文字特效代码
- CSS 图片横向排列实现代码
- C#实现将Email地址转成图片显示的方法
- 超级经典一套鼠标控制左右滚动图片带自动翻滚
- 用css实现图片垂直居中的使用技巧
- nodejs中的fiber(纤程)库详解
- C++实现读取图片长度和宽度
- 桌面中心(一)创建数据库
- php通过修改header强制图片下载的方法
- PHP安全上传图片的方法
- 使用GD库生成带阴影文字的图片
- php获取网页里所有图片并存入数组的方法
- c#实现图片二值化例子(黑白效果)