获取本地相册图片
2014-03-03 10:20
295 查看
.h
< UIPickerViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,strong)NSString *filePath;
@property(nonatomic,strong)UIActionSheet *myActionSheet ;
.m
//获取相册和本地图片
-(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.sourceType = sourceType;
[self
presentModalViewController:picker animated:YES];
}else
{
NSLog(@"模拟其中无法打开照相机,请在真机中使用");
}
}
//打开本地相册
-(void)LocalPhoto
{
UIImagePickerController *picker = [[UIImagePickerController
alloc] init];
picker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary; picker.delegate =
self;
//设置选择后的图片可被编辑
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"];
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);
}
< UIPickerViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,strong)NSString *filePath;
@property(nonatomic,strong)UIActionSheet *myActionSheet ;
.m
//获取相册和本地图片
-(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.sourceType = sourceType;
[self
presentModalViewController:picker animated:YES];
}else
{
NSLog(@"模拟其中无法打开照相机,请在真机中使用");
}
}
//打开本地相册
-(void)LocalPhoto
{
UIImagePickerController *picker = [[UIImagePickerController
alloc] init];
picker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary; picker.delegate =
self;
//设置选择后的图片可被编辑
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"];
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);
}
相关文章推荐
- IOS博客项目搭建-16-发微博-点击button获取相册图片
- 工具类—相册获取照片
- oracle复制表结构和复制表数据语句分享
- centos 6.2升级6.5遇到的问题及解决方法
- JAVA反射机制
- ControlTemplate和DataTemplate的区别
- Eclipse Class Decompiler——Java反编译插件
- iis功能要求, COM+目录要求, asp.net版本注册要求
- Arrays.sort()排序
- Visual Studio 跨平台开发实战(1) - Hello Xamarin!
- 一台电脑同时运行多个tomcat配置方法
- JqueryMobile链接一个页面,而链接页面中图片需刷新才显示的问题
- 第三章 少量(无)标记增强现实——Chapter 3:Marker-less Augmented Reality
- net重量 cross重量
- [Esri官方声明]ArcGIS产品的支持生命周期
- DedeCMS arclist标签中实现表格及列表中两列排列
- 数据结构 -- 查找之 二分法查找
- android Theme使用三
- Web Service入门
- Apache配置中文域名