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
最新文章
- C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 远程同步服务器大量基础数据到客户端
- 关于css
- android 程序代码执行adb
- Error
- iOS下日期的处理(世界标准时转本地时间)
- http://blog.csdn.net/superhosts/article/details/15813247
- important的妙用
- MySQL 关闭子表的外键约束检察
- postgresql 在linux上的源码安装
- Asp.net mvc 中的路由
- 使用QGIS将文本坐标转换为矢量文件
- Kafka作为大数据的核心技术,你了解多少?
- 关于git merge,rebase合并的差别,以及*(no branch)的处理。
- python 列表 元祖 集合
- POJ Lost Cows
- C++点和箭头操作符用法区别
- 安全测试===sqlmap(壹)转载
- Winform下让你的DataGridView控件支持点语法(即显示list中的子对象属性)
- Asp.Net Session的三种方法及Web.Config设置
- Hadoop大数据处理读书笔记
热门文章
- dcos下rexray服务的配置
- 【旧文章搬运】Win7 OBJECT_HEADER之TypeIndex解析
- getElementsByName()获取标签时的注意
- SyntaxError: can&#39;t assign to operator
- 【网络爬虫】【java】微博爬虫(二):如何抓取HTML页面及HttpClient使用
- 4-3逻辑非运算符及案例 &; 4-4
- C# 程序软件启动默认管理员权限。
- C#中的explicit和implicit了解一下吧
- unity gl 画线
- MongoDb 抛出";Error retrieving nonce";异常