#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文件

#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==1)
[self shootPiicturePrVideo];
else if(buttonIndex==2)
[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]>0){
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, 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, 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超全开源框架、项目和学习资料汇总--数据库、缓存处理、图像浏览、摄像照相视频音频篇

    iOS超全开源框架.项目和学习资料汇总--数据库.缓存处理.图像浏览.摄像照相视频音频篇 感谢:Ming_en_long 的分享 大神超赞的集合,http://www.jianshu.com/p/f3 ...

  2. iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像

    //弹出actionsheet.选择获取头像的方式 //从相册获取图片 -(void)takePictureClick:(UIButton *)sender { // /*注:使用,需要实现以下协议: ...

  3. 开源 iOS 项目分类索引大全 - 待整理

    开源 iOS 项目分类索引大全 GitHub 上大概600个开源 iOS 项目的分类和介绍,对于你挑选和使用开源项目应该有帮助 系统基础库 Category/Util sstoolkit 一套Cate ...

  4. iOS 相机

    本章节主要为之前项目 JXHomepwner 添加照片功能(项目地址).具体任务就是显示一个 UIImagePickerController 对象,使用户能够为 JXItem 对象拍照并保存.拍摄的照 ...

  5. ios项目里扒出来的json文件

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0. ...

  6. Github上关于iOS的各种开源项目集合(强烈建议大家收藏,查看,总有一款你需要)

    下拉刷新 EGOTableViewPullRefresh - 最早的下拉刷新控件. SVPullToRefresh - 下拉刷新控件. MJRefresh - 仅需一行代码就可以为UITableVie ...

  7. 关于近期项目代码整理(iOS)

    近期对项目中所经常使用到的封装代码进行整理,并将其上传至网络保存,本人会在后期不间断的更新其内容.具体链接地址为代码封装 关于代码 这些代码为从学习iOS来到现在实际项目开发中,精炼出来的封装代码,使 ...

  8. 史上最全的常用iOS的第三方框架

    文章来源:http://blog.csdn.net/sky_2016/article/details/45502921 图像: 1.图片浏览控件MWPhotoBrowser       实现了一个照片 ...

  9. ios审核要注意的地方(转)

    磨刀不误砍柴工.作为手机应用开发者,你需要向应用商店提交应用审核,迅速通过审核可以让你抢占先机.对苹果iOS应用开发者来说尤其如此.苹果应用商店的审核近乎吹毛求疵,下面这些清单可以让你知道苹果会在哪些 ...

随机推荐

  1. SIP协议&开源SIP服务器搭建和客户端安装

    1. SIP SIP 是一个应用层的控制协议,可以用来建立,修改,和终止多媒体会话,例如Internet电话 SIP在建立和维持终止多媒体会话协议上,支持五个方面: 1)   用户定位: 检查终端用户 ...

  2. android性能测试工具

    Android性能测试工具Emmagee介绍   Emmagee介绍 Emmagee是监控指定被测应用在使用过程中占用机器的CPU.内存.流量资源的性能测试小工具.该工具的优势在于如同windows系 ...

  3. 开发板启动时,内核打印出"can't access tty,job control turned off"

    启动后的最后一行提示can't access tty,job control turned off, 这说明没有进入到控制台,原因就在于文件系统的/etc/inittab 这个文件里有问题 vi /e ...

  4. SSH框架搭建的时候遇到的问题

    1.spring-web.jar包问题:使用user libaries方式,识别不到,于是出现了下面问题 java.lang.ClassNotFoundException: org.springfra ...

  5. 20个Flutter实例视频教程-第10节: 一个不简单的搜索条-1

    20个Flutter实例视频教程-第10节: 一个不简单的搜索条-1 视频地址: https://www.bilibili.com/video/av39709290/?p=10 博客地址: https ...

  6. DOM学习笔记(二)对象方法与属性

    所有 HTML 元素被定义为对象,而编程接口(对象的访问)则是对象方法和对象属性. 事实上,常用的只用window对象及其子对象document对象,以及事件Event对象. Window 对象 Wi ...

  7. Linux下配置 禅道 (项目管理系统)

    1.官网下载源码 2.解析一个新的子域名,然后到linux服务器上新建一个站点 3.将下载的文件上传至linux服务器(新建站点的时候已经在 /home/wwwroot/ 里建好了域名对应的文件夹,将 ...

  8. NITACMOJ144稳定串

    点我>>题目链接 稳定串 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java ...

  9. Untiy一些方法前特殊标签记录

    [ExecuteInEditMode] // Make code live-update even when not in play mode [ContextMenu("Execute&q ...

  10. IT兄弟连 JavaWeb教程 JSP内置对象经典面试题

    1.请说明cookie.request.session.application的作用域和声明周期? 并说明它们适用与什么场景? request的生命周期是一次请求.可以用于JSP表单提交数据. ses ...