IOS开发---菜鸟学习之路--(十五)-如何实现拍照功能
本章将来讲解下如何实现拍照的功能
我们需要的实现的效果是
好了 直接开始内容吧
首先我们需要新建一个ViewController
就叫AddPictureViewController
然后选择.h文件进行如下修改
- #import <UIKit/UIKit.h>
- @interface AddPictureViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>
- {
- UITextView *contenttextview;
- UIImageView *contentimageview;
- NSString *lastChosenMediaType;
- }
- @property(nonatomic,retain) IBOutlet UITextView *contenttextview;
- @property(nonatomic,retain) IBOutlet UIImageView *contentimageview;
- @property(nonatomic,copy) NSString *lastChosenMediaType;
- -(IBAction)buttonclick:(id)sender;
- @end
AddPictureViewController.h
我们需要添加以下两个库
QuartzCore
MobileCoreServices
在项目的General里找到 linked Frameworks and libraries 添加两个类库
然后修改XIB文件
并建立相关链接
最后就是.m文件部分的代码了。
我就直接上代码了 大家可以直接复制过去 然后再理解
- #import "AddPictureViewController.h"
- #import <QuartzCore/QuartzCore.h>
- #import <QuartzCore/CoreAnimation.h>
- #import <MobileCoreServices/UTCoreTypes.h>
- @interface AddPictureViewController ()
- @end
- @implementation AddPictureViewController
- @synthesize contentimageview;
- @synthesize contenttextview;
- @synthesize lastChosenMediaType;
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- }
- -(IBAction)buttonclick:(id)sender
- {
- UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"请选择图片来源" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从手机相册选择", nil];
- [alert show];
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma 拍照选择模块
- -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
- if(buttonIndex==)
- [self shootPiicturePrVideo];
- else if(buttonIndex==)
- [self selectExistingPictureOrVideo];
- }
- #pragma mark- 拍照模块
- //从相机上选择
- -(void)shootPiicturePrVideo{
- [self getMediaFromSource:UIImagePickerControllerSourceTypeCamera];
- }
- //从相册中选择
- -(void)selectExistingPictureOrVideo{
- [self getMediaFromSource:UIImagePickerControllerSourceTypePhotoLibrary];
- }
- #pragma 拍照模块
- -(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
- self.lastChosenMediaType=[info objectForKey:UIImagePickerControllerMediaType];
- if([lastChosenMediaType isEqual:(NSString *) kUTTypeImage])
- {
- UIImage *chosenImage=[info objectForKey:UIImagePickerControllerEditedImage];
- contentimageview.image=chosenImage;
- }
- if([lastChosenMediaType isEqual:(NSString *) kUTTypeMovie])
- {
- UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示信息!" message:@"系统只支持图片格式" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
- [alert show];
- }
- [picker dismissModalViewControllerAnimated:YES];
- }
- -(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
- [picker dismissModalViewControllerAnimated:YES];
- }
- -(void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType
- {
- NSArray *mediatypes=[UIImagePickerController availableMediaTypesForSourceType:sourceType];
- if([UIImagePickerController isSourceTypeAvailable:sourceType] &&[mediatypes count]>){
- NSArray *mediatypes=[UIImagePickerController availableMediaTypesForSourceType:sourceType];
- UIImagePickerController *picker=[[UIImagePickerController alloc] init];
- picker.mediaTypes=mediatypes;
- picker.delegate=self;
- picker.allowsEditing=YES;
- picker.sourceType=sourceType;
- NSString *requiredmediatype=(NSString *)kUTTypeImage;
- NSArray *arrmediatypes=[NSArray arrayWithObject:requiredmediatype];
- [picker setMediaTypes:arrmediatypes];
- [self presentModalViewController:picker animated:YES];
- }
- else{
- UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"错误信息!" message:@"当前设备不支持拍摄功能" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
- [alert show];
- }
- }
- static UIImage *shrinkImage(UIImage *orignal,CGSize size)
- {
- CGFloat scale=[UIScreen mainScreen].scale;
- CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
- CGContextRef context=CGBitmapContextCreate(NULL, size.width *scale,size.height*scale, , , colorSpace, kCGImageAlphaPremultipliedFirst);
- CGContextDrawImage(context, CGRectMake(, , size.width*scale, size.height*scale), orignal.CGImage);
- CGImageRef shrunken=CGBitmapContextCreateImage(context);
- UIImage *final=[UIImage imageWithCGImage:shrunken];
- CGContextRelease(context);
- CGImageRelease(shrunken);
- return final;
- }
- @end
AddPictureViewController.m
最后就实现效果了
IOS开发---菜鸟学习之路--(十五)-如何实现拍照功能的更多相关文章
- IOS开发---菜鸟学习之路--(五)-MacBook购买前后感想
前几天刚入手了一台MACBOOK AIR 13寸 13版的 这几天使用过来个人感觉还是非常不错的. 这几天每天晚上都抱着她玩到十一.二点. 今天晚上突然想起来好久没续写博客了.就连忙开始码字了. 此章 ...
- IOS开发---菜鸟学习之路--(二十二)-近期感想以及我的IOS学习之路
在不知不觉当中已经写了21篇内容 其实一开始是没有想些什么东西的 只是买了Air后 感觉用着挺舒服的,每天可以躺在床上,就一台笔记本,不用网线,不用电源,不用鼠标,不用键盘,干干脆脆的就一台笔记本. ...
- IOS开发---菜鸟学习之路--(一)
PS(废话): 看了那么多的博客文章,发现大部分人都一直在强调写技术博客的重要性,索性自己也耐着性子写写看吧. 写博客的重要性之类的说明,我就不做复制黏贴的工作了.因为自己没有写过多少,所也不清楚是不 ...
- IOS开发---菜鸟学习之路--(二十三)-直接利用键值对的方式来处理数据的感想
首先声明,本文纯粹只是做为本人个人新手的理解.文中的想法我知道肯定有很多地方是错的. 但是这就是我作为一个新人的使用方法,对于大牛非常欢迎指导,对于喷子请绕道而行. 由于这是早上跟我学长讨论数据处理时 ...
- IOS开发---菜鸟学习之路--(十七)-利用UITableView实现个人信息界面
首先来看下我们要实现的效果 需要实现这样的效果 然后我们开始动手吧. 首先选择添加一个新的ViewController 然后打开XIB文件,添加一UITableView 并将样式设置为分组 同时将按住 ...
- IOS开发---菜鸟学习之路--(二十四)-iOS7View被导航栏遮挡问题的解决
(此文为复制帖,原文地址为:http://blog.sina.com.cn/s/blog_a8192bdd0101af40.html) self.navigationController.naviga ...
- IOS开发---菜鸟学习之路--(二十)-二维码扫描功能的实现
本章将讲解如何实现二维码扫描的功能 首先在github上下载ZBar SDK地址https://github.com/bmorton/ZBarSDK 然后将如下的相关类库添加进去 AVFoundati ...
- IOS开发---菜鸟学习之路--(十九)-利用NSUserDefaults存储数据
利用NSUserDefaults的可以快速的进行本地数据存储,但是支持的格式有限, 至于支持什么格式大家可以再自行脑补 我这边直接讲如何使用 NSUserDefaults 分为两部分 一个是存数据 N ...
- IOS开发---菜鸟学习之路--(十八)-利用代理实现向上一级页面传递数据
其实我一开始是想实现微信的修改个人信息那样的效果 就是点击昵称,然后跳转到另外一个页面输入信息 但是细想发现微信的话应该是修改完一个信息后就保存了 而我做的项目可能需要输入多个数据之后再点击提交的. ...
随机推荐
- iOS .Crash文件分析处理办法 (利用symbolicatecrash工具处理)
崩溃分析方式:命令行解析Crash文件 通过Mac自带的命令行工具解析Crash文件需要具备三个文件 symbolicatecrash,Xcode自带的崩溃分析工具,使用这个工具可以更精确的定位崩溃所 ...
- 关于第三方dll,ocx开发的思考
A问题: 最近有个工作,要集成一套老的指纹考勤机器到现在考勤系统(web系统)中,问题出现时老的机器只有ocx可用,没有可用的dll:原本以为简单的第三方调用就ok了,可是ocx不能被承载,在实现上费 ...
- sql server2016安装程序图
今天终于有时间安装SQL Server2016正式版,下载那个安装包都用了一个星期 安装包可以从这里下载: http://www.itellyou.cn/ https://msdn.microsoft ...
- 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] 方案 ...
- 编译防火墙——C++的Pimpl惯用法解析
http://blog.csdn.net/lihao21/article/details/47610309 Pimpl(pointer to implementation, 指向实现的指针)是一种常用 ...
- RAC基本使用
@interface ViewController () @property (weak, nonatomic) IBOutlet lwRedView *redView; @property (wea ...
- 3203 数组做函数参数----排序函数--C语言版
3203: 数组做函数参数----排序函数--C语言版 时间限制: 1 Sec 内存限制: 128 MB提交: 253 解决: 151[提交][状态][讨论版][命题人:smallgyy] 题目描 ...
- 2017.12.6 计算机算法分析与设计---------Fibonacci数列
(1)题目: 无穷数列1,1,2,3,5,8,13,21,34,55,--,称为Fibonacci数列.它可以递归地定义为: 第n个Fibonacci数可递归地计算如下: int fibonacci( ...
- 解决linux系统CentOS下调整home和根分区大小《转》
转自http://www.php114.net/2013/1019/637.html 目标:将VolGroup-lv_home缩小到20G,并将剩余的空间添加给VolGroup-lv_root 1 ...
- 安装Ubuntu桌面环境后只能Guest登录的解决办法
1.安装Ubuntu桌面环境后,登录界面只显示了Guest 2.在登录界面按住crtl+shift+F1,进入tty模式 3.输入sudo -s进入root模式 4.输入vi /etc/lightdm ...