今天给大家带来调取相机的 Demo
#import <UIKit/UIKit.h>
@interface TestViewController : UIViewController
{
//输入框
UITextView *_textEditor;
//下拉菜单
UIActionSheet *myActionSheet;
//图片2进制路径
NSString* filePath;
}
@end
.m:
#import "TestViewController.h"
@interface TestViewController ()<UITextViewDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@end
@implementation TestViewController
- (void)viewDidLoad {
[superviewDidLoad];
//导航栏标题
self.navigationItem.title =@"Amydom";
//导航栏按钮
self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc]initWithTitle:@"发送"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(sendInfo)] ;
//输入框显示区域
_textEditor = [[UITextViewalloc] initWithFrame:CGRectMake(0,0, 320,100)];
//设置它的代理
_textEditor.delegate =self;
_textEditor.autoresizingMask =UIViewAutoresizingFlexibleWidth;
_textEditor.keyboardType =UIKeyboardTypeDefault;
_textEditor.font = [UIFontsystemFontOfSize:20];
_textEditor.text =@"请输入内容";
//默认软键盘是在触摸区域后才会打开
//这里表示进入当前ViewController直接打开软键盘
[_textEditorbecomeFirstResponder];
//把输入框加在视图中
[self.viewaddSubview:_textEditor];
//下方的图片按钮点击后呼出菜单 打开摄像机查找本地相册
UIImage *image = [[UIImagealloc] initWithContentsOfFile:[[NSBundlemainBundle] pathForResource:@"camera"ofType:@"png"]];
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 120, image.size.width, image.size.height);
[button setImage:imageforState:UIControlStateNormal];
[button addTarget:selfaction:@selector(openMenu)forControlEvents:UIControlEventTouchUpInside];
//把它也加在视图当中
[self.viewaddSubview:button];
}
-(void)openMenu
{
//在这里呼出下方菜单按钮项
myActionSheet = [[UIActionSheetalloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles: @"打开照相机",@"从手机相册获取",nil];
[myActionSheetshowInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//呼出的菜单按钮点击后的响应
if (buttonIndex == myActionSheet.cancelButtonIndex)
{
NSLog(@"取消");
}
switch (buttonIndex)
{
case 0: //打开照相机拍照
[self takePhoto];
break;
case 1: //打开本地相册
[self LocalPhoto];
break;
}
}
//开始拍照
-(void)takePhoto
{
UIImagePickerControllerSourceType sourceType =UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerControllerisSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[UIImagePickerControlleralloc] init];
picker.delegate = self;
//设置拍照后的图片可被编辑
picker.allowsEditing = YES;
picker.sourceType = sourceType;
[selfpresentModalViewController:picker animated:YES];
}else
{
NSLog(@"模拟其中无法打开照相机,请在真机中使用");
}
}
//打开本地相册
-(void)LocalPhoto
{
UIImagePickerController *picker = [[UIImagePickerControlleralloc] init];
picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
//设置选择后的图片可被编辑
picker.allowsEditing = YES;
[selfpresentModalViewController:picker animated:YES];
}
//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *type = [infoobjectForKey:UIImagePickerControllerMediaType];
//当选择的类型是图片
if ([type isEqualToString:@"public.image"])
{
//先把图片转成NSData
UIImage* image = [infoobjectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *data;
if (UIImagePNGRepresentation(image) ==nil)
{
data = UIImageJPEGRepresentation(image,1.0);
}
else
{
data = UIImagePNGRepresentation(image);
}
//图片保存的路径
//这里将图片放在沙盒的documents文件夹中
NSString * DocumentsPath = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"];
//文件管理器
NSFileManager *fileManager = [NSFileManagerdefaultManager];
//把刚刚图片转换的data对象拷贝至沙盒中并保存为image.png
[fileManager createDirectoryAtPath:DocumentsPathwithIntermediateDirectories:YESattributes:nilerror:nil];
[fileManager createFileAtPath:[DocumentsPathstringByAppendingString:@"/image.png"]contents:data attributes:nil];
//得到选择后沙盒中图片的完整路径
filePath = [[NSStringalloc]initWithFormat:@"%@%@",DocumentsPath, @"/image.png"];
//关闭相册界面
[picker dismissModalViewControllerAnimated:YES];
//创建一个选择后图片的小图标放在下方
//类似微薄选择图后的效果
UIImageView *smallimage = [[UIImageViewalloc] initWithFrame:
CGRectMake(50,120, 40, 40)] ;
smallimage.image = image;
//加在视图中
[self.viewaddSubview:smallimage];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
NSLog(@"您取消了选择图片");
[picker dismissModalViewControllerAnimated:YES];
}
-(void)sendInfo
{
NSLog(@"图片的路径是:%@",filePath);
NSLog(@"您输入框中的内容是:%@",_textEditor.text);
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}