#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. android 通过包名过滤logcat

    #!/bin/bash ]]; then cat <<EOF Usage: `` <packagename> EOF exit package_name=$ ip= pid_l ...

  2. js搜索相同类型的控件全选、取值(Checkbox)

    function selectAll(obj) { if (obj.checked) { $("input[type='checkbox']").each(function () ...

  3. elmo驱动器使用EAS II配置教程

    一.驱动器接线 1.1驱动器接口: 驱动器接线,需要连接两个接口,一个是反馈接口,一个是STO接口. 反馈接口,我这里使用了elmo驱动器的Port A.这个接口提供5v电源.并且可以输入旋转编码器和 ...

  4. 怎么在Ubuntu下设置程序的快捷键

    参考 http://jingyan.baidu.com/article/1e5468f97f9e75484861b773.html 我的系统是 64bit Ubuntu14.04 我设置了 gedit ...

  5. 8、html的body内标签之fieldset标签和label标签

    一.label标签 <label> 标签为 input 元素定义标签(label). label 元素不会向用户呈现任何特殊的样式.不过,它为鼠标用户改善了可用性,因为如果用户点击 lab ...

  6. Cpython 解释器下实现并发编程

    背景知识: 顾明思议: 进程即正在执行的一个过程,进程是对正在运行程序的一个抽象. 进程的概念起源于操作系统, 是操作系统最核心的概念,也是操作系统提供的最古老的最重要的抽象概念之一.操作系统的其他所 ...

  7. 为何在有 DOCTYPE 的 HTML 文档之上仍然还会出现混杂模式?

    不使用 DOCTYPE 一定会使 HTML 文档处于混杂模式,然而使用了 DOCTYPE,也不一定就能够使文档在所有浏览器中均处于标准模式. DOCTYPE 本身不就是一个“开关”吗?为何在有 DOC ...

  8. ORA-22992:没法使用从远程表选择的LOB定位器

    OLB 问题 ORA-22992:没法使用从远程表选择的LOB定位器 Create global temporary table temp on commit preserve rows as sel ...

  9. C/C++内存检测工具Valgrind

    内存检测Valgrind简介 Valgrind是运行在Linux上一套基于仿真技术的程序调试和分析工具,作者是获得过Google-O'Reilly开源大奖的Julian Seward, 它包含一个内核 ...

  10. E20180709-hm

    extract  vt. 提取; (费力地) 拔出; 选取; 获得