IOS 从系统图库中获取 图片 并设置为头像
总会遇到一些应用 要拍照 设置图片之类的的 总结一个方法备用 以后 会 继续拓展到 多图
@interface AddCustomerViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate> { UIImage *chosenImage;//做显示在self.view 上的image NSString *tempImagePath;//图片路径 UIButton *button_Photo;//点击从系统选图片 } @end //以上代理 和参数 都是必须有的
-(void)TakePhoto { UIActionSheet *actionSheet =[[UIActionSheet alloc]initWithTitle:@"您想如何获取照片?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", nil]; [actionSheet showInView:self.view]; } -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { /* NSLog(@"%ld",(long)buttonIndex);//2--->取消 UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; if(buttonIndex==0) {//拍照 picker.sourceType=UIImagePickerControllerSourceTypeCamera; } else if(buttonIndex==1) {//图库 picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; } [self presentViewController:picker animated:YES completion:NULL]; */ NSLog(@"%ld",(long)buttonIndex);//2--->取消 if(buttonIndex==0) {//拍照 UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType=UIImagePickerControllerSourceTypeCamera; [self presentViewController:picker animated:YES completion:NULL]; } } #pragma -mark UIImagePickerController delegate -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { chosenImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; //删除缓存文件 if ([[NSFileManager defaultManager] fileExistsAtPath:tempImagePath]) { NSLog(@"the image is exist"); NSFileManager *defaultManager; defaultManager = [NSFileManager defaultManager]; NSError *error = [[NSError alloc] init]; [defaultManager removeItemAtPath:tempImagePath error:&error]; } [UIImagePNGRepresentation(chosenImage) writeToFile:[ NSTemporaryDirectory() stringByAppendingPathComponent:@"temp_image.png"] atomically:YES]; BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:tempImagePath]; if (fileExists) { NSLog(@"success"); customer_Inform.image_Head=chosenImage; [button_Photo setImage:chosenImage forState:UIControlStateNormal]; } [picker dismissViewControllerAnimated:YES completion:NULL]; if (fileExists) { NSLog(@"success"); [SGInfoAlert showInfo:@" 照片添加成功! " bgColor:[[UIColor darkGrayColor] CGColor] inView:self.view vertical:0.5]; } else { [SGInfoAlert showInfo:@" 照片添加不成功! " bgColor:[[UIColor darkGrayColor] CGColor] inView:self.view vertical:0.5]; } // //也能用 // [button_Photo setImage:[info objectForKey:@"UIImagePickerControllerOriginalImage"] forState:UIControlStateNormal]; // [picker dismissViewControllerAnimated:YES completion:NULL]; }
posted on 2014-04-15 18:52 ACM_Someone like you 阅读(621) 评论(0) 编辑 收藏 举报
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)