iOS 开发调用相机以及获取相册照片功能
2017-06-13 14:16
639 查看
//添加代理方法
@interface MineViewController () <UITableViewDelegate, UITableViewDataSource, PayCellDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate>
//定义消息框
UIActionSheet * act =[[UIActionSheet alloc]initWithTitle:@"请选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"选择相册",@"选择相机", nil];
//显示消息框
[act showInView:self.view];
//消息框代理实现
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
//定义图片选择器
UIImagePickerController * picker = [[UIImagePickerController alloc]init];
//判断
switch (buttonIndex) {
case 0:
//判断系统是否允许选择 相册
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
//图片选择是相册(图片来源自相册)
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//设置代理
picker.delegate=self;
//模态显示界面
[self presentViewController:picker animated:YES completion:nil];
}
break;
case 1://判断系统是否允许选择 相机
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//图片选择是相册(图片来源自相册)
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//设置代理
picker.delegate=self;
//模态显示界面
[self presentViewController:picker animated:YES completion:nil];
}
else {
NSLog(@"不支持相机");
}
break;
default:
break;
}
}
//实现图片选择器代理
//参数:图片选择器 字典参数
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//通过key值获取到图片
UIImage * image =info[UIImagePickerControllerOriginalImage];
NSLog(@"image=%@ info=%@",image, info);
//判断数据源类型
if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
//设置图片背景
[userIconBtn setBackgroundImage:image forState:UIControlStateNormal];
NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
NSData* imgData = UIImageJPEGRepresentation(image, 1);
isPhotoChoose = YES;
NSLog(@"第一次调用这个方法");
[user setObject:imgData forKey:@"saveIcon"];
[user synchronize];
[self dismissViewControllerAnimated:YES completion:nil];
}
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
NSLog(@"在相机中选择图片");
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
//设置图片背景
[userIconBtn setBackgroundImage:image forState:UIControlStateNormal];
NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
NSData* imgData = UIImageJPEGRepresentation(image, 1);
isPhotoChoose = YES;
NSLog(@"第一次调用这个方法");
[user setObject:imgData forKey:@"saveIcon"];
[user synchronize];
[self dismissViewControllerAnimated:YES completion:nil];
}
}
@interface MineViewController () <UITableViewDelegate, UITableViewDataSource, PayCellDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate>
//定义消息框
UIActionSheet * act =[[UIActionSheet alloc]initWithTitle:@"请选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"选择相册",@"选择相机", nil];
//显示消息框
[act showInView:self.view];
//消息框代理实现
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
//定义图片选择器
UIImagePickerController * picker = [[UIImagePickerController alloc]init];
//判断
switch (buttonIndex) {
case 0:
//判断系统是否允许选择 相册
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
//图片选择是相册(图片来源自相册)
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//设置代理
picker.delegate=self;
//模态显示界面
[self presentViewController:picker animated:YES completion:nil];
}
break;
case 1://判断系统是否允许选择 相机
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//图片选择是相册(图片来源自相册)
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//设置代理
picker.delegate=self;
//模态显示界面
[self presentViewController:picker animated:YES completion:nil];
}
else {
NSLog(@"不支持相机");
}
break;
default:
break;
}
}
//实现图片选择器代理
//参数:图片选择器 字典参数
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//通过key值获取到图片
UIImage * image =info[UIImagePickerControllerOriginalImage];
NSLog(@"image=%@ info=%@",image, info);
//判断数据源类型
if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
//设置图片背景
[userIconBtn setBackgroundImage:image forState:UIControlStateNormal];
NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
NSData* imgData = UIImageJPEGRepresentation(image, 1);
isPhotoChoose = YES;
NSLog(@"第一次调用这个方法");
[user setObject:imgData forKey:@"saveIcon"];
[user synchronize];
[self dismissViewControllerAnimated:YES completion:nil];
}
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
NSLog(@"在相机中选择图片");
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
//设置图片背景
[userIconBtn setBackgroundImage:image forState:UIControlStateNormal];
NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
NSData* imgData = UIImageJPEGRepresentation(image, 1);
isPhotoChoose = YES;
NSLog(@"第一次调用这个方法");
[user setObject:imgData forKey:@"saveIcon"];
[user synchronize];
[self dismissViewControllerAnimated:YES completion:nil];
}
}
相关文章推荐
- iOS开发:调用系统自带相机以及获取相册照片的功能实现
- iOS 开发调用相机以及获取相册照片功能
- iOS开发-调用系统相机和相册获取照片示例
- Android调用系统相机、相册功能,适配6.0权限获取以及7.0以后获取URI(兼容多版本)
- iOS开发swift如何调用系统相册和相机获取图片设置用户头像
- [置顶] Android 7.0实际开发中调用系统相机和获取相册照片遇到的坑具备向下兼容
- Android 7.0实际开发中调用系统相机和获取相册照片遇到的坑具备向下兼容
- ios 调用系统相机,图库,相册。将照片保存在相册,保存图片在本地,UIImagePickerController
- iOS开发中调用系统相册、相机 显示中文标题
- iOS 调用系统相机和选择相册照片
- java微信开发API解析(三)-高级功能的前奏----获取以及保存接口调用凭证
- iOS开发 调用系统相机和相册
- Android Camera开发系列(上)——Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片
- android开发之上传图片和调用相机以及相册
- iOS开发:相册或相机调用
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时为什么会回到home界面或者到前一个界面,然后才会跳转到当前activity
- iOS开发调用相机拍摄多张照片
- iOS开发 调用系统相机和相册
- iOS调用系统相机,访问系统相册功能实现
- Android开发,调用相册图片或者调用相机进行拍照获取图片