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

我们需要的实现的效果是

    

好了 直接开始内容吧

首先我们需要新建一个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开发---菜鸟学习之路--(十五)-如何实现拍照功能的更多相关文章

  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. Error: unknown argument: '-websockets'

    参考原文:http://www.cocoachina.com/bbs/read.php?tid=194014 解决方法:点击项目右边编辑区域上面有一个building setting找到other l ...

  2. genlist -s 192.168.21.\*

    显示网段192.168.21中可用的主机.

  3. Visual Studio 2015 Preview 使用中问题一枚

    只要碰到IO读写,文件不存在之类的系统异常,就会崩溃一下给你看看.直接重新VS. 不该有的问题确实存在着???? 正常情况是这样的 直接崩溃时万万不行的!!!!

  4. mustache.js 数组循环的索引

    在使用mustache作为模板引擎时,想要利用数组中的对象的索引排序,却发现mustache中无法获得数组索引,在一番搜索之后,发现在数组的对象中加入索引,就可以了,示例如下 /html {{#dat ...

  5. 使用VSCode搭建TypeScript开发环境 (重点)

    下载TypeScript 在CMD(Windows系统)或者终端(macOS系统)中输入一下命令: npm install -g typescript 下载VSCode VSCode是我使用过最棒的编 ...

  6. AngularJs学习笔记-数据绑定、管道

    数据绑定.管道 (1)数据绑定(Angular中默认是单向绑定) 1.[]方括号 可以用于子组件传值 由于是单向绑定,所以当子组件中的iStars属性发生改变时,不会影响到父组件中product.ra ...

  7. Drupal的入门学习

    1. 注意content中的区别 Article和Basic page的区别 a.输入字段不一样,Article内容多了两个字段:tag和图片. b.内容的默认设置不一样,Article默认允许评论, ...

  8. Java代码随机生成图片验证码

    package com.rchm.util.images; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2 ...

  9. Shell脚本使用汇总整理——达梦数据库备份脚本

    Shell脚本使用汇总整理——达梦数据库备份脚本 Shell脚本使用的基本知识点汇总详情见连接: https://www.cnblogs.com/lsy-blogs/p/9223477.html 脚本 ...

  10. Linux IO调度方法

    目录 I/O调度的4种算法 I/O调度程序的测试 ionice IO调度器的总体目标是希望让磁头能够总是往一个方向移动,移动到底了再往反方向走,这恰恰就是现实生活中的电梯模型,所以IO调度器也被叫做电 ...