从本地选取相册和相机拍照

转载自:http://blog.csdn.net/l_ch_g/article/details/11738129点击打开链接

//开始拍照
090 -(void)takePhoto
091 {
092     UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
093     if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
094     {
095         UIImagePickerController *picker = [[UIImagePickerController alloc] init];
096         picker.delegate = self;
097         //设置拍照后的图片可被编辑
098         picker.allowsEditing = YES;
099         picker.sourceType = sourceType;
100         [picker release];
101         [self presentModalViewController:picker animated:YES];
102     }else
103     {
104         NSLog(@"模拟其中无法打开照相机,请在真机中使用");
105     }
106 }
107  
108 //打开本地相册
109 -(void)LocalPhoto
110 {
111     UIImagePickerController *picker = [[UIImagePickerController alloc] init];
112  
113     picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
114     picker.delegate = self;
115     //设置选择后的图片可被编辑
116     picker.allowsEditing = YES;
117     [self presentModalViewController:picker animated:YES];
118     [picker release];
119 }
120  
121 //当选择一张图片后进入这里
122 -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
123  
124 {
125  
126     NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
127  
128     //当选择的类型是图片
129     if ([type isEqualToString:@"public.image"])
130     {
131         //先把图片转成NSData
132         UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
133         NSData *data;
134         if (UIImagePNGRepresentation(image) == nil)
135         {
136             data = UIImageJPEGRepresentation(image, 1.0);
137         }
138         else
139         {
140             data = UIImagePNGRepresentation(image);
141         }
142  
143         //图片保存的路径
144         //这里将图片放在沙盒的documents文件夹中
145         NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 
146  
147         //文件管理器
148         NSFileManager *fileManager = [NSFileManager defaultManager];
149  
150         //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
151         [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
152         [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
153  
154         //得到选择后沙盒中图片的完整路径
155         filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath,  @"/image.png"];
156  
157         //关闭相册界面
158         [picker dismissModalViewControllerAnimated:YES];
159  
160         //创建一个选择后图片的小图标放在下方
161         //类似微薄选择图后的效果
162         UIImageView *smallimage = [[[UIImageView alloc] initWithFrame:
163                                    CGRectMake(50, 120, 40, 40)] autorelease];   
164  
165         smallimage.image = image;
166         //加在视图中
167         [self.view addSubview:smallimage];
168  
169     }
170  
171 }
172  
173 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
174 {
175     NSLog(@"您取消了选择图片");
176     [picker dismissModalViewControllerAnimated:YES];
177 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值