iOS ---打开iPhone照相机和相册

本文介绍如何在iOS应用中实现图片选择功能,包括通过摄像头拍摄照片和从相册中选取图片,并展示图片到按钮上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://blog.csdn.net/riven_wn/article/details/46458293




一.添加代理

[objc]  view plain copy
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;color:#333333;"><UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate></span>  

二.添加一个显示图片的按钮

[objc]  view plain copy
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;color:#333333;">- (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.       
  5.     self.view.backgroundColor = [UIColor whiteColor];  
  6.     button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  7.     button.frame = CGRectMake(1001008080);  
  8.     button.backgroundColor = [UIColor redColor];  
  9.     [button addTarget:self action:@selector(addImage) forControlEvents:UIControlEventTouchUpInside];  
  10.     [self.view addSubview:button];  
  11. }  
  12.   
  13. - (void)addImage  
  14. {  
  15.     UIActionSheet* sheet = [[UIActionSheet alloc] initWithTitle:@"选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从摄像头",@"从图片库",nil];  
  16.     [sheet showInView:self.view];  
  17. }</span>  

三.UIActionSheetDelegate,对应的打开照相机和相册

[objc]  view plain copy
  1. #pragma mark -- UIActionSheetDelegate  
  2. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex  
  3. {  
  4.     switch (buttonIndex) {  
  5.         case 0:  
  6.         {  
  7.             //拍照  
  8.             //资源类型为照相机  
  9.             UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;  
  10.             //判断是否有相机  
  11.             if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){  
  12.                 UIImagePickerController *picker = [[UIImagePickerController alloc] init];  
  13.                 picker.delegate = self;  
  14.                 //设置拍照后的图片可被编辑  
  15.                 picker.allowsEditing = YES;  
  16.                 //资源类型为照相机  
  17.                 picker.sourceType = sourceType;  
  18.                 [self presentViewController:picker animated:YES completion:nil];  
  19.                   
  20.             }else {  
  21.                 NSLog(@"该设备无摄像头");  
  22.             }  
  23.         }  
  24.               
  25.             break;  
  26.         case 1:  
  27.         {  
  28.             //从相册选择  
  29.             UIImagePickerController *picker = [[UIImagePickerController alloc] init];  
  30.             //资源类型为图片库  
  31.             picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  32.             picker.delegate = self;  
  33.             //设置选择后的图片可被编辑  
  34.             picker.allowsEditing = YES;  
  35.             //    [self presentModalViewController:picker animated:YES];  
  36.             [self presentViewController:picker animated:YES completion:nil];  
  37.         }  
  38.             break;  
  39.         default:  
  40.             break;  
  41.     }  
  42. }  

四.图像选取器的委托方法,选完图片后回调该方法

[objc]  view plain copy
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;color:#333333;">#pragma Delegate method UIImagePickerControllerDelegate  
  2. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{  
  3.       
  4.     //当图片不为空时显示图片并保存图片  
  5.     if (image != nil) {  
  6.         //图片显示在界面上  
  7.         [button setBackgroundImage:image forState:UIControlStateNormal];  
  8.     }  
  9.     //关闭相册界面  
  10.     [picker dismissModalViewControllerAnimated:YES];  
  11. }</span
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值