iOS选照片
2015-12-24 17:45
519 查看
<UITextViewDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
//输入框
UITextView *_textEditor;
//下拉菜单
UIActionSheet *myActionSheet;
//图片2进制路径
NSString* filePath;
}
/////////////
- (void)openMenu {
//在这里呼出下方菜单按钮项
myActionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles: @"打开照相机", @"从手机相册获取",nil];
[myActionSheet showInView: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 ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
//设置拍照后的图片可被编辑
picker.allowsEditing = YES;
// picker.showsCameraControls = YES;
picker.sourceType = sourceType;
[self presentModalViewController:picker animated:YES];
}else
{
NSLog(@"模拟其中无法打开照相机,请在真机中使用");
}
}
//打开本地相册
-(void)LocalPhoto
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
//设置选择后的图片可被编辑
// picker.showsCameraControls = YES;
picker.allowsEditing = YES;
[self presentModalViewController:picker animated:YES];
}
//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
//当选择的类型是图片
if ([type isEqualToString:@"public.image"])
{
//先把图片转成NSData
// UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
//获取图片裁剪的图
UIImage* image = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *data;
if (UIImagePNGRepresentation(image) == nil)
{
data = UIImageJPEGRepresentation(image, 1.0);
}
else
{
data = UIImagePNGRepresentation(image);
}
//图片保存的路径
//这里将图片放在沙盒的documents文件夹中
NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
[fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
//得到选择后沙盒中图片的完整路径
filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath, @"/image.png"];
//关闭相册界面
[picker dismissModalViewControllerAnimated:YES];
//创建一个选择后图片的小图标放在下方
//类似微薄选择图后的效果
UIImageView *smallimage = [[UIImageView alloc] initWithFrame:
CGRectMake(50, 120, 40, 40)] ;
smallimage.image = image;
//加在视图中
[self.view addSubview:smallimage];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
NSLog(@"您取消了选择图片");
[picker dismissModalViewControllerAnimated:YES];
}
-(void)sendInfo
{
NSLog(@"图片的路径是:%@", filePath);
NSLog(@"您输入框中的内容是:%@", _textEditor.text);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
{
//输入框
UITextView *_textEditor;
//下拉菜单
UIActionSheet *myActionSheet;
//图片2进制路径
NSString* filePath;
}
/////////////
- (void)openMenu {
//在这里呼出下方菜单按钮项
myActionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles: @"打开照相机", @"从手机相册获取",nil];
[myActionSheet showInView: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 ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
//设置拍照后的图片可被编辑
picker.allowsEditing = YES;
// picker.showsCameraControls = YES;
picker.sourceType = sourceType;
[self presentModalViewController:picker animated:YES];
}else
{
NSLog(@"模拟其中无法打开照相机,请在真机中使用");
}
}
//打开本地相册
-(void)LocalPhoto
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
//设置选择后的图片可被编辑
// picker.showsCameraControls = YES;
picker.allowsEditing = YES;
[self presentModalViewController:picker animated:YES];
}
//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
//当选择的类型是图片
if ([type isEqualToString:@"public.image"])
{
//先把图片转成NSData
// UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
//获取图片裁剪的图
UIImage* image = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *data;
if (UIImagePNGRepresentation(image) == nil)
{
data = UIImageJPEGRepresentation(image, 1.0);
}
else
{
data = UIImagePNGRepresentation(image);
}
//图片保存的路径
//这里将图片放在沙盒的documents文件夹中
NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
[fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
//得到选择后沙盒中图片的完整路径
filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath, @"/image.png"];
//关闭相册界面
[picker dismissModalViewControllerAnimated:YES];
//创建一个选择后图片的小图标放在下方
//类似微薄选择图后的效果
UIImageView *smallimage = [[UIImageView alloc] initWithFrame:
CGRectMake(50, 120, 40, 40)] ;
smallimage.image = image;
//加在视图中
[self.view addSubview:smallimage];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
NSLog(@"您取消了选择图片");
[picker dismissModalViewControllerAnimated:YES];
}
-(void)sendInfo
{
NSLog(@"图片的路径是:%@", filePath);
NSLog(@"您输入框中的内容是:%@", _textEditor.text);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
相关文章推荐
- IOS版添加phonegap--语音识别合成插件教程
- iOS开发-百度地图基本定位和罗盘跟随的使用,附加地理编码和反地理编码
- ios 视频断点续传常识
- 一个设置多个类似标志的好办法 iOS 类似flag
- iOS完整学习路线图
- iOS开发-同一段文字显示不同颜色
- iOS上级往下级传值
- iOS 图片圆角
- fiddler ios 手机抓包
- iOS如何判断NSDictionary是否包含某个key
- iOS怎么防止文件被Icloud或者iTunes备份
- iOS开发之单元测试
- IOS 横屏打开相册
- iOS 9开发小技巧
- iOS 编译c程序
- iOS/OS X内存管理:借助工具解决内存问题
- iOS--- Autolayout 基础
- IOS开发之----两种保存用户名和密码实现记住密码库
- iOS开发 .bundle使用
- iOS NSNotificationCenter 使用姿势详解