IOS照相
#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照相的更多相关文章
- iOS超全开源框架、项目和学习资料汇总--数据库、缓存处理、图像浏览、摄像照相视频音频篇
iOS超全开源框架.项目和学习资料汇总--数据库.缓存处理.图像浏览.摄像照相视频音频篇 感谢:Ming_en_long 的分享 大神超赞的集合,http://www.jianshu.com/p/f3 ...
- iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像
//弹出actionsheet.选择获取头像的方式 //从相册获取图片 -(void)takePictureClick:(UIButton *)sender { // /*注:使用,需要实现以下协议: ...
- 开源 iOS 项目分类索引大全 - 待整理
开源 iOS 项目分类索引大全 GitHub 上大概600个开源 iOS 项目的分类和介绍,对于你挑选和使用开源项目应该有帮助 系统基础库 Category/Util sstoolkit 一套Cate ...
- iOS 相机
本章节主要为之前项目 JXHomepwner 添加照片功能(项目地址).具体任务就是显示一个 UIImagePickerController 对象,使用户能够为 JXItem 对象拍照并保存.拍摄的照 ...
- 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. ...
- Github上关于iOS的各种开源项目集合(强烈建议大家收藏,查看,总有一款你需要)
下拉刷新 EGOTableViewPullRefresh - 最早的下拉刷新控件. SVPullToRefresh - 下拉刷新控件. MJRefresh - 仅需一行代码就可以为UITableVie ...
- 关于近期项目代码整理(iOS)
近期对项目中所经常使用到的封装代码进行整理,并将其上传至网络保存,本人会在后期不间断的更新其内容.具体链接地址为代码封装 关于代码 这些代码为从学习iOS来到现在实际项目开发中,精炼出来的封装代码,使 ...
- 史上最全的常用iOS的第三方框架
文章来源:http://blog.csdn.net/sky_2016/article/details/45502921 图像: 1.图片浏览控件MWPhotoBrowser 实现了一个照片 ...
- ios审核要注意的地方(转)
磨刀不误砍柴工.作为手机应用开发者,你需要向应用商店提交应用审核,迅速通过审核可以让你抢占先机.对苹果iOS应用开发者来说尤其如此.苹果应用商店的审核近乎吹毛求疵,下面这些清单可以让你知道苹果会在哪些 ...
随机推荐
- docker容器firewalld端口转发规则
docker容器firewalld端口转发规则 1.配置firewalld端口转发,要先打开端口转发,则需要先 #firewall-cmd --zone=public --add-maspuerade ...
- 【原】Cache Buffer Chain 第四篇
作者:david_zhang@sh [转载时请以超链接形式标明文章] 链接:http://www.cnblogs.com/david-zhang-index/p/3873357.html [测试1]低 ...
- HDOJ-2054
A == B ? Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- Asset Catalog Help (四)---Adding an iOS App Icon Set or Launch Image Set
Adding an iOS App Icon Set or Launch Image Set Organize different resolutions of your app icons and ...
- PHP实用小程序(四)
<HTML> <HEAD> <TITLE>访问文件时间属性</TITLE> </HEAD> <BODY> <? $Last ...
- Win7 server2008 共享文件夹 不输入网络密码
如何设置共享文件夹访问不需要输入用户名和密码: 1.“开始”——“运行”——secpol.msc (1)使用空白密码的本地帐户只允许进行控制台登录 禁用它 (2)“本地策略”——“安全选项”——“网络 ...
- E20190215-mt
parenthesis n. 圆括号; 插入语; 插入成分; 间歇; (parentheses) individual adj. 个人的; 个别的; 独特的; n. 个人; 个体; priva ...
- unity调用Android的jar包
简介 有一些手机功能,Unity没有提供相应的接口,例如震动,例如不锁屏,例如GPS,例如... 有太多的特殊功能Unity都没有提供接口,这时候,我们就需要通过使用Android原生的ADT编辑器去 ...
- bzoj3771: Triple(容斥+生成函数+FFT)
传送门 咳咳忘了容斥了-- 设\(A(x)\)为斧头的生成函数,其中第\(x^i\)项的系数为价值为\(i\)的斧头个数,那么\(A(x)+A^2(x)+A^3(x)\)就是答案(于是信心满满的打了一 ...
- Ruby编程实践
命令 常量大写 类名和模块名首字母大写,驼峰法,MyClass,Person 方法名小写,ruby中末尾添加符号特殊含义:destroyMethod!表示这个方法具有破坏性:isPrime?表示返回b ...