iphone 中的图片选取器 UIImagePickerController以前没有怎么看,现在看了看,总结下
【一】初始化
UIImagePickerController就是普通的初始化,最重要的就是那个 sourceType 这个属性
UIImagePickerController *pick=[[UIImagePickerController alloc]init];
UIImagePickerControllerSourceTypePhotoLibrary:表示显示所有的照片
UIImagePickerControllerSourceTypeCamera:表示从摄像头选取照片
UIImagePickerControllerSourceTypeSavedPhotosAlbum:表示仅仅从相册中选取照片。
【二】UIImagePickerController的使用
UIImagePickerController有三个代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo //ios3之后已经抛弃不再使用- (void)imagePickerController:(UIImagePickerController )picker didFinishPickingMediaWithInfo:(NSDictionary )info;
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
<br/>
因为第一个在 ios2和 ios3之后都不用了,所以主要用后面的两个,还有就是在切换的时候最新的使用presentViewController这个方法
【三】方法示例
主要的代码是这样的,demo 里面是完整的代码,如果需要可以下载 demo,这里主要说下主要功能
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 160, 40)];
[button setTitle:@"点击测试相机" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[self.view addSubview:button];
[button addTarget:self action:@selector(imagefun) forControlEvents:UIControlEventTouchUpInside];
imageview =[[UIImageView alloc]initWithFrame:CGRectMake(100, 150, 200, 200)];
[self.view addSubview:imageview];
}
-(void)imagefun
{
UIActionSheet *action=[[UIActionSheet alloc]initWithTitle:@"选择功能" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"相机" otherButtonTitles:@"相册",nil];
[action showInView:self.view];
}
//actionsheet 总共有两个功能,相册和相机
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//主要是使用这个controller
UIImagePickerController *pick=[[UIImagePickerController alloc]init];
if (buttonIndex==0) {
NSLog(@"相机");
//判断相机是否可用,因为模拟机是不可以的
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
pick.sourceType=UIImagePickerControllerSourceTypeCamera;//设置 pick 的类型为相机
pick.allowsEditing=true;//设置是否可以编辑相片涂鸦
pick.delegate=self;
[self presentViewController:pick animated:true completion:nil];
}
else
{
NSLog(@"相机不可用");
}
}
else if (buttonIndex==1)
{
NSLog(@"相册");
//判断相册是否可用
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
pick.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
pick.allowsEditing=true;
pick.delegate=self;
[self presentViewController:pick animated:true completion:nil];
}
else
{
NSLog(@"相册不可用");
}
}
else
{
NSLog(@"取消");
}
}
///代理方法
- (void)imagePickerController:(UIImagePickerController )picker didFinishPickingMediaWithInfo:(NSDictionary )info
{
NSLog(@"xuanze");
NSString *type=[info objectForKey:UIImagePickerControllerMediaType];
//判断选择的是否是图片,这个 public.image和public.movie是固定的字段.
if ([type isEqualToString:@"public.image"])
{
//其实这样就已经实现了设置图片的功能
UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
//这一步主要是判断当是用相机拍摄的时候,保存到相册
if (picker.sourceType==UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
}
[imageview setImage:image];
//这里再加个写入到本地的方案
//获取写入的文件夹
NSString *documents=[NSHomeDirectory() stringByAppendingString:@"/Documents"];
//也可以用下面这个,区别就是加不加/
//NSString *documents=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//将 image 转换为 data
NSData *data;
if (UIImagePNGRepresentation(image)) {
data=UIImagePNGRepresentation(image);
}
else
data=UIImageJPEGRepresentation(image, 1.0);
NSFileManager *filemanager=[NSFileManager defaultManager];
//避免覆盖
static int i=0;
i++;
[filemanager createFileAtPath:[documents stringByAppendingFormat:@"/%d.png",i] contents:data attributes:nil];
}
//判断选择的是否是视频
if ([type isEqualToString:@"public.movie"]) {
}
[picker dismissViewControllerAnimated:false completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
NSLog(@"取消");
[picker dismissViewControllerAnimated:false completion:nil];
}
【四】demo下载
Github下载:https://github.com/DamonHu/hudongImagePicker
GitOsc下载:http://git.oschina.net/DamonHoo/hudongImagePicker
版权属于:胡东东博客
本文链接:http://blog.hudongdong.com/oc/142.html
自2017年12月26日起,『转载以及大段采集进行后续编辑』须注明本文标题和链接!否则禁止所有转载和采集行为!
☟☟如文章有用,可点击一次下方广告支持一下☟☟