课程表

iOS课程

工具箱
速查手册

相机管理

当前位置:免费教程 » 移动开发 » iOS

相机简介

相机是移动设备的共同特点之一,我们能够使用相机拍摄图片,并在应用程序里调用它,而且相机的使用很简单。

实例步骤

1、创建一个简单的View based application

2、在ViewController.xib中添加一个button (按钮),并为该按钮创建IBAction

3、添加一个 image view (图像视图),并创建一个名为imageView的IBOutlet

4、ViewController.h文件代码如下所示:

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface ViewController : UIViewController<UIImagePickerControllerDelegate>
  4. {
  5. UIImagePickerController *imagePicker;
  6. IBOutlet UIImageView *imageView;
  7. }
  8. - (IBAction)showCamera:(id)sender;
  9.  
  10. @end

5、修改ViewController.m,如下所示:

  1. #import "ViewController.h"
  2.  
  3. @interface ViewController ()
  4.  
  5. @end
  6.  
  7. @implementation ViewController
  8.  
  9. - (void)viewDidLoad
  10. {
  11. [super viewDidLoad];
  12. }
  13.  
  14. - (void)didReceiveMemoryWarning
  15. {
  16. [super didReceiveMemoryWarning];
  17. // Dispose of any resources that can be recreated.
  18. }
  19.  
  20. - (IBAction)showCamera:(id)sender {
  21. imagePicker.allowsEditing = YES;
  22. if ([UIImagePickerController isSourceTypeAvailable:
  23. UIImagePickerControllerSourceTypeCamera])
  24. {
  25. imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
  26. }
  27. else{
  28. imagePicker.sourceType =
  29. UIImagePickerControllerSourceTypePhotoLibrary;
  30. }
  31. [self presentModalViewController:imagePicker animated:YES];
  32.  
  33. }
  34. -(void)imagePickerController:(UIImagePickerController *)picker
  35. didFinishPickingMediaWithInfo:(NSDictionary *)info{
  36. UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
  37. if (image == nil) {
  38. image = [info objectForKey:UIImagePickerControllerOriginalImage];
  39. }
  40. imageView.image = image;
  41. }
  42.  
  43. -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
  44. [self dismissModalViewControllerAnimated:YES];
  45. }
  46.  
  47. @end

输出

运行该应用程序并单击显示相机按钮时,我们就会获得下面的输出

camera_Output1

只要拍照之后,就可以通过移动和缩放对图片进行编辑,如下所示。

camera_Output2

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号