iOS调用相机,相册,上传头像
一、新建工程
二、拖控件,创建映射
三、在.h中加入delegate
- @interface ViewController : UIViewController
复制代码
四、实现按钮事件
- -(IBAction)chooseImage:(id)sender {
- UIActionSheet *sheet;
- // 判断是否支持相机
- if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
- {
- sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选择", nil];
- }
- else {
- sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册选择", nil];
- }
- sheet.tag = 255;
- [sheet showInView:self.view];
- }
复制代码
五、实现actionSheet delegate事件
- -(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
- {
- if (actionSheet.tag == 255) {
- NSUInteger sourceType = 0;
- // 判断是否支持相机
- if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
- switch (buttonIndex) {
- case 0:
- // 取消
- return;
- case 1:
- // 相机
- sourceType = UIImagePickerControllerSourceTypeCamera;
- break;
- case 2:
- // 相册
- sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- break;
- }
- }
- else {
- if (buttonIndex == 0) {
- return;
- } else {
- sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
- }
- }
- // 跳转到相机或相册页面
- UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
- imagePickerController.delegate = self;
- imagePickerController.allowsEditing = YES;
- imagePickerController.sourceType = sourceType;
- [self presentViewController:imagePickerController animated:YES completion:^{}];
- [imagePickerController release];
- }
- }
复制代码
六、实现ImagePicker delegate 事件
- #pragma mark - image picker delegte
- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
- {
- [picker dismissViewControllerAnimated:YES completion:^{}];
- UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
- /* 此处info 有六个值
- * UIImagePickerControllerMediaType; // an NSString UTTypeImage)
- * UIImagePickerControllerOriginalImage; // a UIImage 原始图片
- * UIImagePickerControllerEditedImage; // a UIImage 裁剪后图片
- * UIImagePickerControllerCropRect; // an NSValue (CGRect)
- * UIImagePickerControllerMediaURL; // an NSURL
- * UIImagePickerControllerReferenceURL // an NSURL that references an asset in the AssetsLibrary framework
- * UIImagePickerControllerMediaMetadata // an NSDictionary containing metadata from a captured photo
- */
- // 保存图片至本地,方法见下文
- [self saveImage:image withName:@"currentImage.png"];
- NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];
- UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
- isFullScreen = NO;
- [self.imageView setImage:savedImage];
- self.imageView.tag = 100;
- }
- - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
- {
- [self dismissViewControllerAnimated:YES completion:^{}];
- }
复制代码
七、保存图片
高保真压缩图片方法
- NSData * UIImageJPEGRepresentation ( UIImage *image, CGFloat compressionQuality
- )
复制代码
此方法可将图片压缩,但是图片质量基本不变,第二个参数即图片质量参数。
- #pragma mark - 保存图片至沙盒
- - (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
- {
- NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
- // 获取沙盒目录
- NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
- // 将图片写入文件
- [imageData writeToFile:fullPath atomically:NO];
- }
复制代码
八、实现点击图片预览功能,滑动放大缩小,带动画
- -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- {
- isFullScreen = !isFullScreen;
- UITouch *touch = [touches anyObject];
- CGPoint touchPoint = [touch locationInView:self.view];
- CGPoint imagePoint = self.imageView.frame.origin;
- //touchPoint.x ,touchPoint.y 就是触点的坐标
- // 触点在imageView内,点击imageView时 放大,再次点击时缩小
- if(imagePoint.x <= touchPoint.x && imagePoint.x +self.imageView.frame.size.width >=touchPoint.x && imagePoint.y <= touchPoint.y && imagePoint.y+self.imageView.frame.size.height >= touchPoint.y)
- {
- // 设置图片放大动画
- [UIView beginAnimations:nil context:nil];
- // 动画时间
- [UIView setAnimationDuration:1];
- if (isFullScreen) {
- // 放大尺寸
- self.imageView.frame = CGRectMake(0, 0, 320, 480);
- }
- else {
- // 缩小尺寸
- self.imageView.frame = CGRectMake(50, 65, 90, 115);
- }
- // commit动画
- [UIView commitAnimations];
- }
- }
复制代码
九、上传图片,使用ASIhttpRequest类库实现,由于本文重点不是网络请求,故不对ASIHttpRequest详细讲述,只贴出部分代码
- ASIFormDataRequest *requestReport = [[ASIFormDataRequest alloc] initWithURL:服务器地址];
- NSString *Path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];
- [requestReport setFile:Path forKey:@"picturepath"];
- [requestReport buildPostBody];
- requestReport.delegate = self;
- [requestReport startAsynchronous];
复制代码
iOS调用相机,相册,上传头像的更多相关文章
- iOS调用相机,相册,上传头像 分类: ios技术 2015-04-14 11:23 256人阅读 评论(0) 收藏
一.新建工程 二.拖控件,创建映射 三.在.h中加入delegate @interface ViewController : UIViewController 复制代码 四.实现按钮事件 -(IBAc ...
- IOS调用相机相册
#import "SendViewController.h" //只能打开,没有加载图片的代码,老代码,供参考 #import <MobileCoreServices/UT ...
- 相册选择头像或者拍照 上传头像以NSData 图片二进制格式 表单上传
一.点击头像图片 或者按钮 在相册选择照片返回img,网络上传头像要用data表单上传 (1)上传头像属性 // 图片二进制格式 表单上传 @property (nonatomic, strong) ...
- 个人信息——头像更换(拍照或相册上传)~ 微信小程序
微信小程序中 在用户信息中关于用户头像更换(拍照或相册上传)功能实现. 图像点击触发事件: <image src='{{personImage}}' bindtap='changeAvatar' ...
- swift上传头像
很久没有写博客了,今天特地写了这个,也是一边仿照别人写的demo,注释部分都是需要的.需要的同学可以参考一下. @IBAction func headImageBtnPage(){ //上传头像 / ...
- day105:Mofang:设置页面初始化&更新头像/上传头像&设置页面显示用户基本信息
目录 1.设置页面初始化 2.更新头像 1.点击头像进入更新头像界面 2.更新头像页面初始化 3.更新头像页面CSS样式 4.头像上传来源选择:相册/相机 5.调用api提供的本地接口从相册/相机提取 ...
- apiCloud上传头像
apiCloud上传头像 1.拍照 2.从相机中选择 aui布局 <li class="aui-list-item"> <div class="aui- ...
- 完美实现类似QQ的自拍头像、上传头像功能!(Demo 源码)
现在很多下载客户端程序都需要设定自己头像的功能,而设定头像一般有两种方式:使用摄像头自拍头像,或者选择一个图片的某部分区域作为自己的头像. 一.相关技术 若要实现上述的自拍头像和上传头像的功能,会碰到 ...
- Jcrop+uploadify+php实现上传头像预览裁剪
最近由于项目需要,所以做了一个上传头像预览并且可以预览裁剪的功能,大概思路是上传的图片先保存到服务器,然后通过ajax从服务器获取到图片信息,再利用Jcrop插件进行裁剪,之后通过PHP获取到的四个裁 ...
随机推荐
- 在win7/8/10鼠标右键添加带管理员权限的“在此处打开命令窗口”
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Drive\shell\runas]@="@shell32.dll,-8506 ...
- 在win7/8/10鼠标右键添加“管理员取得所有权”
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\Shell\TakeAuthority]"icon"=" ...
- AI 人工智能 探索 (八)
绑定下,用来释放内存 布局框架.链接:http://pan.baidu.com/s/1eQzSXZO 密码:25ir 这次 我采用 ngui 来设定界面.除工具栏模块外,其他各类ui模块都是 内存池动 ...
- HDOJ--ACM-Steps--2.1.3--Cake(GCD,简单数学)
一次生日Party可能有p人或者q人参加,现准备有一个大蛋糕.问最少要将蛋糕切成多少块(每块大小不一定相等),才能使p人或者q人出席的任何一种情况,都能平均将蛋糕分食. Input 每行有两个数p和q ...
- android 5.0新特性学习--视图阴影
android 5.0的视图阴影主要是体验出层次性,就是在一个物体上面叠加上一层的设计,而这种设计就是除了传统的,x,y的纸面层,还有就是透过纸面的z轴的层次设计. elevation: 高度,静态属 ...
- 【spring boot】SpringBoot初学(2) - properties配置和读取
前言 只是简单的properties配置学习,修改部分"约定"改为自定义"配置".真正使用和遇到问题是在细看. 一.主要 核心只是demo中的: @Proper ...
- 使用Java管理千台规模Linux服务器_入门
http://www.oschina.net/code/snippet_222919_11734 代码分享 当前位置: 代码分享 » Java » 网络编程 搜 索 [饶过] 使用Java管理千 ...
- AJAX开发技术--AJAX简介
Asynchronous JavaScript and XML,异步JavaScript和XML 主要目的用于页面的局部刷新.不用全部刷新,提高性能. 在AJAX中主要是通过XMLHttpReque ...
- Ubuntu系统如何修改主机名
1.执行命令 hostname temp_name 这样主机名就改掉了.只不过重启后名字会恢复不一定使我们想要的.机器重启后会重新去读取/etc/hostname里面存储的主机名.所以如果想永久改掉的 ...
- Android ---paint类
引自:http://www.cnblogs.com/-OYK/archive/2011/10/25/2223624.html Android Paint和Color类 要绘图,首先得调整画笔,待画 ...