iPhone手机拍照功能和照片库选取功能调用

浅谈iPhone手机拍照功能和照片库选取功能调用是本文要介绍的内容,内容不多,主要是以代码实现,我们来看内容。

首先,你得实现UIImagePickerControllerDelegate,UINavigationControllerDelegate两个代理

其次定义按钮:

// 从相簿里上传一张

  1. UIButton *btn1 = [UIButton buttonWithType: UIButtonTypeRoundedRect];
  2. btn1.frame = CGRectMake(60.0, 237.0, 200.0, 32.0);
  3. [btn1 setTitle:@"从媒体库中选取图片" forState:UIControlStateNormal];
  4. [btn1 addTarget:self action:@selector(picker) forControlEvents:UIControlEventTouchUpInside];
  5. UIButton *btn2 = [UIButton buttonWithType: UIButtonTypeRoundedRect];
  6. btn2.frame = CGRectMake(60.0, 290.0, 200.0, 32.0);
  7. [btn2 setTitle:@"从相机获取图片" forState:UIControlStateNormal];
  8. [btn2 addTarget:self action:@selector(picker1) forControlEvents:UIControlEventTouchUpInside];

实现按钮方法:

  1. // 从照片库里面选取
  2. -(void)picker
  3. {
  4. imagepicker = [[UIImagePickerController alloc] init];
  5. imagepicker.delegate = self;
  6. imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  7. imagepicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
  8. imagepicker.allowsEditing = YES;
  9. [self presentModalViewController:self.imagepicker animated:YES];
  10. }
  11. // 从相机获取
  12. -(void)picker1
  13. {
  14. imagepicker = [[UIImagePickerController alloc] init];
  15. imagepicker.delegate = self;
  16. imagepicker.sourceType = UIImagePickerControllerSourceTypeCamera;
  17. imagepicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
  18. imagepicker.allowsEditing = YES;
  19. [self presentModalViewController:self.imagepicker animated:YES];
  20. }

实现代理方法:

  1. // 初始化选取
  2. - (void)imagePickerController:(UIImagePickerController *)picker
  3. didFinishPickingImage:(UIImage *)image
  4. editingInfo:(NSDictionary *)editingInfo
  5. {
  6. myImageView.image = image;
  7. [[picker parentViewController] dismissModalViewControllerAnimated:YES];
  8. }
  9. // 完成选取
  10. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  11. {
  12. [picker dismissModalViewControllerAnimated:YES];
  13. }
posted @ 2012-04-05 16:59  sherry~  阅读(183)  评论(0)    收藏  举报