iOS 头像裁剪上传
打开本地相册或打开本地相机拍照,获取图片裁剪图片,上传图片
加入头文件
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
- 1
- 2
- 3
- 1
- 2
- 3
全部变量
NSString*THE_IMG_PATH;
NSString *_imageString
- 1
- 2
- 1
- 2
添加代理
<VPImageCropperDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
- 1
- 1
相关代码
#pragma mark 用户头像点击响应的方法,- (void)getImageFrom{UIActionSheet *choiceSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从相册中选取", nil];[choiceSheet showInView:self.view];
}#pragma mark UIActionSheetDelegate 点击ActionSheet 选择头像来源相册或者相机- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {if (buttonIndex == 0) {// 来源相机-拍照
NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];if(authStatus == ALAuthorizationStatusRestricted ||authStatus == ALAuthorizationStatusDenied){
//不能访问相机
NSLog(@"相机权限受限");
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"访问相机失败" message:@"请打开 设置-隐私-相机 来进行设置" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];return;}else{
//设置可以访问相机
if ([UIImagePickerOrCamera isCameraAvailable] && [UIImagePickerOrCamera doesCameraSupportTakingPhotos]) {UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerOrCamera isFrontCameraAvailable]) {controller.cameraDevice =UIImagePickerControllerCameraDeviceFront;
}
NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
[mediaTypes addObject:(__bridge NSString *)kUTTypeImage];controller.mediaTypes = mediaTypes;
controller.delegate = self;
[self presentViewController:controller animated:YES completion:^(void){
NSLog(@"Picker View Controller is presented");}];}}} else if (buttonIndex == 1) {//来源相册——访问相册
ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];if (author == ALAuthorizationStatusRestricted || author ==ALAuthorizationStatusDenied) {
//设置不能访问相册UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"访问相册失败" message:@"请打开 设置-隐私-照片 来进行设置" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];}else{
//设置可以访问相册
if ([UIImagePickerOrCamera isPhotoLibraryAvailable]) {UIImagePickerController *controller = [[UIImagePickerController alloc] init];controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
[mediaTypes addObject:(__bridge NSString *)kUTTypeImage];controller.mediaTypes = mediaTypes;
controller.delegate = self;
[self presentViewController:controller animated:YES completion:^(void){NSLog(@"Picker View Controller is presented");}];}} }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
#pragma mark - 相册导航器的 cancle的响应,退出相册- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:^(){
//TO Do
}];
}//对相册的导航条修改
#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];if ([navigationController isKindOfClass:[UIImagePickerController class]] &&((UIImagePickerController *)navigationController).sourceType ==UIImagePickerControllerSourceTypePhotoLibrary) {[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];}
}#pragma mark 设置相册导航控制器的 导航模式
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
-
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
//裁剪
#pragma mark UIImagePickerControllerDelegate 裁剪图片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//相册退出
[picker dismissViewControllerAnimated:YES completion:^() {UIImage *portraitImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
portraitImg = [UIImagePickerOrCamera imageByScalingToMaxSize:portraitImg];// 裁剪
VPImageCropperViewController *imgEditorVC = [[VPImageCropperViewController alloc] initWithImage:portraitImg cropFrame:CGRectMake(0, 100.0f, self.view.frame.size.width, self.view.frame.size.width) limitScaleRatio:3.0];imgEditorVC.delegate = self;[self presentViewController:imgEditorVC animated:YES completion:^{
// TO DO
}];
}];
}#pragma mark 取消裁剪
- (void)imageCropperDidCancel:(VPImageCropperViewController *)cropperViewController {[cropperViewController dismissViewControllerAnimated:YES completion:^{}];
}#pragma mark VPImageCropperDelegate 裁剪完成,对图片压缩处理
- (void)imageCropper:(VPImageCropperViewController *)cropperViewController didFinished:(UIImage *)editedImage {//存储在本地
THE_IMG_PATH=[UIImagePickerOrCamera saveImage:editedImage WithName:@"userHeadImage.png"];//上传小图片,对图片尺寸压缩
editedImage=[UIImagePickerOrCamera imageWithImage:editedImage scaledToSize:CGSizeMake(Main_Screen_Width, Main_Screen_Width)];//图片Gzip压缩
NSData*imageData=UIImagePNGRepresentation(editedImage);//可以对图片进行gzip压缩或者其他相应处理
_imageString=[[NSString alloc]initWithData:imageData encoding:NSUTF8StringEncoding];/***/[cropperViewController dismissViewControllerAnimated:YES completion:^{
// TO DO
/**
图片裁剪完成,可以做相应处理
*/
}];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
图片的上传 这里是用第三方AFNetWorking
-(viod)updata{UIApplication *application = [UIApplication sharedApplication];AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];
manager.requestSerializer.timeoutInterval = 5;
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/javascript",@"text/html", @"application/json",nil];NSArray*arrar=[THE_IMG_PATH componentsSeparatedByString:@"/"];
NSString*imageName=[arrar objectAtIndex:arrar.count-1];
NSMutableDictionary*dic=@{@"image":_imageString,@"imagePath":imageName};[manager POST:urlString parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {if(sucess){sucess(responseObject);NSLog(@"responseObject=%@",responseObject);
}} failure:^(AFHTTPRequestOperation *operation, NSError *error) {NSLog(@"LYYB----AFNetWoring.errno=%@",error); fails(error);}];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
iOS 头像裁剪上传相关推荐
- 基于cropper和sweetalert的简单图片/头像裁剪上传
基本功能 前端基本样式: 进行图片裁剪及上传: 点击上传后,js会将截取到的数据转为图片数据利用ajax发送给后台进行存储.存储成功后,刷新前端页面,头像改变. 上传成功后:自动刷新网页,更改头像 基 ...
- 头像裁剪上传_微信新功能:11月微信新玩法!给微信换上“皇冠头像”,还能加姓氏?...
阅读本文前,请您先点击上面的蓝色字体"电影收录达人",再点击"关注",这样您就可以继续免费收到文章了.每天都有分享,完全是免费订阅,请放心关注. 声明:图文来源 ...
- 头像裁剪上传插件php,PHP+ajaxfileupload+jcrop插件完美实现头像上传剪裁
搜索热词 昨天花了点时间整合了一下头像插件 东拼西凑的成果 先来看下效果 1.先使用ajaxfileupload插件做异步上传.这个地方我本来想做个上传进度的效果,但技术有限失败了.上传按钮我还做了一 ...
- vue vant头像裁剪上传使用笔记
1.安装 cnpm install vue-cropper --save 2.使用 <template><div class="height100">< ...
- jcrop php 头像裁剪,上传裁剪头像(Jcrop)
上传裁剪头像(Jcrop) -- by Stephen 2016/1/24 20:17:21 总体思路:用户点击选择头像按钮,选好图片后jcrop所在的div以遮罩层的形式显示,此时异步更新图片的sr ...
- android仿支付宝头像裁剪,易用的头像裁剪上传、头像美化组件
更新记录 3.4.2(2020-06-30) 参数优化 3.3.4(2020-06-24) 支付宝平台必须填写noTab参数 查看更多 十分抱歉,因为精力有限,本插件暂停维护 注意: 1) H5平台, ...
- 头像裁剪上传插件php,yii2.0 上传头像插件(可裁剪)yii2-avatar扩展包
1.yii2.0 上传头像插件包名 基于cropper的一款头像上传yii2组件 包名: composer require daimakuai/yii2-avatar 2.截图 3.yii2.0 上传 ...
- vue3图片头像裁剪上传
效果图 1.安装vue-cropper pnpm add vue-cropper@next 2.组件内引用 import 'vue-cropper/dist/index.css'//引入圖片裁剪 im ...
- mysql 存储用户头像_node+vue用户头像处理上传并保存
2017年7月14日19:21:29 ,最近做个网站,需要有用户头像裁剪上传功能,具体流程是用户在本地选择图片,前端将图片裁剪好发给后端,后端接收到图片将它保存起来,并将信息存入数据库. 先说一下环境 ...
最新文章
- 前端中的this,指的是什么?
- Cylinder3D :3D环境下的Lidar 点云分割
- 产品运营必须知道的几个概念,什么是跳出率和退出率?
- STM32的启动分析
- python帝国cms_Python的类
- java实现加减乘除运算符随机生成十道题并判断对错_2020年Java面试题(3年的工作总结),最全的知识点总结...
- $Android自定义控件在不同状态下的属性
- html写界面,C++|Qt后台处理业务(后台登录例子JavaScript给Qt提供数据)
- boost采取什么驱动电路_当我们只是采取积极的意愿时会发生什么?
- python通用数据库连接_python 连接数据库pg
- 手把手教会|绘制风控中常用的可视化图表
- 127.0.0.1和0.0.0.0和本机IP的区别
- MySQL引擎详解(三)——InnoDB引擎特性
- jQuery知识点学习整理
- 狂雨小说(KYXS)CMS 代码审计
- IIS无法启动问题的解决
- word2016画函数曲线图
- 神探狄仁杰是一个Debug高手
- OpenCV isContinuous()函数简析
- 论文笔记:Pointing Novel Objects in Image Captioning
热门文章
- 无人机电池能不能上高铁以及什么型号的电池可以上高铁
- 计算机主机只有cpu,装机不求人:电脑CPU坏是什么原因?
- 参与腾讯云数据迁移与视频转码,就有机会赢1000元京东卡!
- 英语翻译中从句的翻译技巧和举例
- 【PPT动画】开头目录动态效果(文末附件)
- Android原生的简单确定取消对话框用法
- 见证奇观(重庆日全食,最美、最好)
- raw文件 linux,Linux下安装RawTherapee,跨平台RAW格式图像处理工具
- 两个数字的交换(指针的使用)
- html字幕字体大小,视频加字幕大小不一样 一般视频字幕字体大小 字幕保存时设置ASS格式 utf-8编码...