iOS 调用相机拍照和选择图库图片 设置头像

不多说,直接上代码


#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];

}



- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}



@end


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值