浅谈iPhone手机拍照功能和照片库选取功能调用是本文要介绍的内容,内容不多,主要是以代码实现,我们来看内容。
首先,你得实现UIImagePickerControllerDelegate,UINavigationControllerDelegate两个代理
其次定义按钮:
- // 从相簿里上传一张
- UIButton *btn1 = [UIButton buttonWithType: UIButtonTypeRoundedRect];
- btn1.frame = CGRectMake(60.0, 237.0, 200.0, 32.0);
- [btn1 setTitle:@"从媒体库中选取图片" forState:UIControlStateNormal];
- [btn1 addTarget:self action:@selector(picker) forControlEvents:UIControlEventTouchUpInside];
- UIButton *btn2 = [UIButton buttonWithType: UIButtonTypeRoundedRect];
- btn2.frame = CGRectMake(60.0, 290.0, 200.0, 32.0);
- [btn2 setTitle:@"从相机获取图片" forState:UIControlStateNormal];
- [btn2 addTarget:self action:@selector(picker1) forControlEvents:UIControlEventTouchUpInside];
实现按钮方法:
- // 从照片库里面选取
- -(void)picker
- {
- imagepicker = [[UIImagePickerController alloc] init];
- imagepicker.delegate = self;
- imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- imagepicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
- imagepicker.allowsEditing = YES;
- [self presentModalViewController:self.imagepicker animated:YES];
- }
- // 从相机获取
- -(void)picker1
- {
- imagepicker = [[UIImagePickerController alloc] init];
- imagepicker.delegate = self;
- imagepicker.sourceType = UIImagePickerControllerSourceTypeCamera;
- imagepicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
- imagepicker.allowsEditing = YES;
- [self presentModalViewController:self.imagepicker animated:YES];
- }
实现代理方法:
- // 初始化选取
- - (void)imagePickerController:(UIImagePickerController *)picker
- didFinishPickingImage:(UIImage *)image
- editingInfo:(NSDictionary *)editingInfo
- {
- myImageView.image = image;
- [[picker parentViewController] dismissModalViewControllerAnimated:YES];
- }
- // 完成选取
- - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
- {
- [picker dismissModalViewControllerAnimated:YES];
- }
小结:浅谈iPhone手机拍照功能和照片库选取功能调用的内容介绍完了,希望本文对你有所帮助