本章将来讲解下如何实现拍照的功能

我们需要的实现的效果是

    

好了 直接开始内容吧

首先我们需要新建一个ViewController

就叫AddPictureViewController

然后选择.h文件进行如下修改

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface AddPictureViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>
  4. {
  5. UITextView *contenttextview;
  6. UIImageView *contentimageview;
  7. NSString *lastChosenMediaType;
  8.  
  9. }
  10. @property(nonatomic,retain) IBOutlet UITextView *contenttextview;
  11. @property(nonatomic,retain) IBOutlet UIImageView *contentimageview;
  12. @property(nonatomic,copy) NSString *lastChosenMediaType;
  13. -(IBAction)buttonclick:(id)sender;
  14.  
  15. @end

AddPictureViewController.h

我们需要添加以下两个库

QuartzCore

MobileCoreServices

在项目的General里找到  linked Frameworks and libraries 添加两个类库

然后修改XIB文件

并建立相关链接

最后就是.m文件部分的代码了。

我就直接上代码了 大家可以直接复制过去 然后再理解

  1. #import "AddPictureViewController.h"
  2. #import <QuartzCore/QuartzCore.h>
  3. #import <QuartzCore/CoreAnimation.h>
  4. #import <MobileCoreServices/UTCoreTypes.h>
  5. @interface AddPictureViewController ()
  6.  
  7. @end
  8.  
  9. @implementation AddPictureViewController
  10. @synthesize contentimageview;
  11. @synthesize contenttextview;
  12. @synthesize lastChosenMediaType;
  13. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  14. {
  15. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  16. if (self) {
  17. // Custom initialization
  18. }
  19. return self;
  20. }
  21.  
  22. - (void)viewDidLoad
  23. {
  24. [super viewDidLoad];
  25. // Do any additional setup after loading the view from its nib.
  26. }
  27. -(IBAction)buttonclick:(id)sender
  28. {
  29. UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"请选择图片来源" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从手机相册选择", nil];
  30. [alert show];
  31. }
  32. - (void)didReceiveMemoryWarning
  33. {
  34. [super didReceiveMemoryWarning];
  35. // Dispose of any resources that can be recreated.
  36. }
  37. #pragma 拍照选择模块
  38. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
  39. if(buttonIndex==)
  40. [self shootPiicturePrVideo];
  41. else if(buttonIndex==)
  42. [self selectExistingPictureOrVideo];
  43. }
  44. #pragma mark- 拍照模块
  45. //从相机上选择
  46. -(void)shootPiicturePrVideo{
  47. [self getMediaFromSource:UIImagePickerControllerSourceTypeCamera];
  48. }
  49. //从相册中选择
  50. -(void)selectExistingPictureOrVideo{
  51. [self getMediaFromSource:UIImagePickerControllerSourceTypePhotoLibrary];
  52. }
  53. #pragma 拍照模块
  54. -(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
  55. self.lastChosenMediaType=[info objectForKey:UIImagePickerControllerMediaType];
  56. if([lastChosenMediaType isEqual:(NSString *) kUTTypeImage])
  57. {
  58. UIImage *chosenImage=[info objectForKey:UIImagePickerControllerEditedImage];
  59. contentimageview.image=chosenImage;
  60. }
  61. if([lastChosenMediaType isEqual:(NSString *) kUTTypeMovie])
  62. {
  63. UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示信息!" message:@"系统只支持图片格式" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
  64. [alert show];
  65.  
  66. }
  67. [picker dismissModalViewControllerAnimated:YES];
  68. }
  69. -(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
  70. [picker dismissModalViewControllerAnimated:YES];
  71. }
  72. -(void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType
  73. {
  74. NSArray *mediatypes=[UIImagePickerController availableMediaTypesForSourceType:sourceType];
  75. if([UIImagePickerController isSourceTypeAvailable:sourceType] &&[mediatypes count]>){
  76. NSArray *mediatypes=[UIImagePickerController availableMediaTypesForSourceType:sourceType];
  77. UIImagePickerController *picker=[[UIImagePickerController alloc] init];
  78. picker.mediaTypes=mediatypes;
  79. picker.delegate=self;
  80. picker.allowsEditing=YES;
  81. picker.sourceType=sourceType;
  82. NSString *requiredmediatype=(NSString *)kUTTypeImage;
  83. NSArray *arrmediatypes=[NSArray arrayWithObject:requiredmediatype];
  84. [picker setMediaTypes:arrmediatypes];
  85. [self presentModalViewController:picker animated:YES];
  86. }
  87. else{
  88. UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"错误信息!" message:@"当前设备不支持拍摄功能" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
  89. [alert show];
  90. }
  91. }
  92. static UIImage *shrinkImage(UIImage *orignal,CGSize size)
  93. {
  94. CGFloat scale=[UIScreen mainScreen].scale;
  95. CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
  96. CGContextRef context=CGBitmapContextCreate(NULL, size.width *scale,size.height*scale, , , colorSpace, kCGImageAlphaPremultipliedFirst);
  97. CGContextDrawImage(context, CGRectMake(, , size.width*scale, size.height*scale), orignal.CGImage);
  98. CGImageRef shrunken=CGBitmapContextCreateImage(context);
  99. UIImage *final=[UIImage imageWithCGImage:shrunken];
  100. CGContextRelease(context);
  101. CGImageRelease(shrunken);
  102. return final;
  103. }
  104.  
  105. @end

AddPictureViewController.m

最后就实现效果了

IOS开发---菜鸟学习之路--(十五)-如何实现拍照功能的更多相关文章

  1. IOS开发---菜鸟学习之路--(五)-MacBook购买前后感想

    前几天刚入手了一台MACBOOK AIR 13寸 13版的 这几天使用过来个人感觉还是非常不错的. 这几天每天晚上都抱着她玩到十一.二点. 今天晚上突然想起来好久没续写博客了.就连忙开始码字了. 此章 ...

  2. IOS开发---菜鸟学习之路--(二十二)-近期感想以及我的IOS学习之路

    在不知不觉当中已经写了21篇内容 其实一开始是没有想些什么东西的 只是买了Air后 感觉用着挺舒服的,每天可以躺在床上,就一台笔记本,不用网线,不用电源,不用鼠标,不用键盘,干干脆脆的就一台笔记本. ...

  3. IOS开发---菜鸟学习之路--(一)

    PS(废话): 看了那么多的博客文章,发现大部分人都一直在强调写技术博客的重要性,索性自己也耐着性子写写看吧. 写博客的重要性之类的说明,我就不做复制黏贴的工作了.因为自己没有写过多少,所也不清楚是不 ...

  4. IOS开发---菜鸟学习之路--(二十三)-直接利用键值对的方式来处理数据的感想

    首先声明,本文纯粹只是做为本人个人新手的理解.文中的想法我知道肯定有很多地方是错的. 但是这就是我作为一个新人的使用方法,对于大牛非常欢迎指导,对于喷子请绕道而行. 由于这是早上跟我学长讨论数据处理时 ...

  5. IOS开发---菜鸟学习之路--(十七)-利用UITableView实现个人信息界面

    首先来看下我们要实现的效果 需要实现这样的效果 然后我们开始动手吧. 首先选择添加一个新的ViewController 然后打开XIB文件,添加一UITableView 并将样式设置为分组 同时将按住 ...

  6. IOS开发---菜鸟学习之路--(二十四)-iOS7View被导航栏遮挡问题的解决

    (此文为复制帖,原文地址为:http://blog.sina.com.cn/s/blog_a8192bdd0101af40.html) self.navigationController.naviga ...

  7. IOS开发---菜鸟学习之路--(二十)-二维码扫描功能的实现

    本章将讲解如何实现二维码扫描的功能 首先在github上下载ZBar SDK地址https://github.com/bmorton/ZBarSDK 然后将如下的相关类库添加进去 AVFoundati ...

  8. IOS开发---菜鸟学习之路--(十九)-利用NSUserDefaults存储数据

    利用NSUserDefaults的可以快速的进行本地数据存储,但是支持的格式有限, 至于支持什么格式大家可以再自行脑补 我这边直接讲如何使用 NSUserDefaults 分为两部分 一个是存数据 N ...

  9. IOS开发---菜鸟学习之路--(十八)-利用代理实现向上一级页面传递数据

    其实我一开始是想实现微信的修改个人信息那样的效果 就是点击昵称,然后跳转到另外一个页面输入信息 但是细想发现微信的话应该是修改完一个信息后就保存了 而我做的项目可能需要输入多个数据之后再点击提交的. ...

随机推荐

  1. iOS .Crash文件分析处理办法 (利用symbolicatecrash工具处理)

    崩溃分析方式:命令行解析Crash文件 通过Mac自带的命令行工具解析Crash文件需要具备三个文件 symbolicatecrash,Xcode自带的崩溃分析工具,使用这个工具可以更精确的定位崩溃所 ...

  2. 关于第三方dll,ocx开发的思考

    A问题: 最近有个工作,要集成一套老的指纹考勤机器到现在考勤系统(web系统)中,问题出现时老的机器只有ocx可用,没有可用的dll:原本以为简单的第三方调用就ok了,可是ocx不能被承载,在实现上费 ...

  3. sql server2016安装程序图

    今天终于有时间安装SQL Server2016正式版,下载那个安装包都用了一个星期 安装包可以从这里下载: http://www.itellyou.cn/ https://msdn.microsoft ...

  4. UVA 10564 Paths through the Hourglass(背包)

    为了方便打印路径,考虑从下往上转移.dp[i][j][S]表示在i行j列总和为S的方案, dp[i][j][S] = dp[i+1][left][S-x]+dp[i+1][right][S-x] 方案 ...

  5. 编译防火墙——C++的Pimpl惯用法解析

    http://blog.csdn.net/lihao21/article/details/47610309 Pimpl(pointer to implementation, 指向实现的指针)是一种常用 ...

  6. RAC基本使用

    @interface ViewController () @property (weak, nonatomic) IBOutlet lwRedView *redView; @property (wea ...

  7. 3203 数组做函数参数----排序函数--C语言版

    3203: 数组做函数参数----排序函数--C语言版 时间限制: 1 Sec  内存限制: 128 MB提交: 253  解决: 151[提交][状态][讨论版][命题人:smallgyy] 题目描 ...

  8. 2017.12.6 计算机算法分析与设计---------Fibonacci数列

    (1)题目: 无穷数列1,1,2,3,5,8,13,21,34,55,--,称为Fibonacci数列.它可以递归地定义为: 第n个Fibonacci数可递归地计算如下: int fibonacci( ...

  9. 解决linux系统CentOS下调整home和根分区大小《转》

    转自http://www.php114.net/2013/1019/637.html 目标:将VolGroup-lv_home缩小到20G,并将剩余的空间添加给VolGroup-lv_root   1 ...

  10. 安装Ubuntu桌面环境后只能Guest登录的解决办法

    1.安装Ubuntu桌面环境后,登录界面只显示了Guest 2.在登录界面按住crtl+shift+F1,进入tty模式 3.输入sudo -s进入root模式 4.输入vi /etc/lightdm ...