转载自:http://www.lvtao.net/ios/509.html

//弹出actionsheet。选择获取头像的方式
//从相册获取图片
-(void)takePictureClick:(UIButton *)sender
{
//    /*注:使用,需要实现以下协议:UIImagePickerControllerDelegate,
//     UINavigationControllerDelegate
//     */
//    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
//    //设置图片源(相簿)
//    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//    //设置代理
//    picker.delegate = self;
//    //设置可以编辑
//    picker.allowsEditing = YES;
//    //打开拾取器界面
//    [self presentViewController:picker animated:YES completion:nil];UIActionSheet* actionSheet = [[UIActionSheet alloc]initWithTitle:@"请选择文件来源"delegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"照相机",@"摄像机",@"本地相簿",@"本地视频",nil];[actionSheet showInView:self.view];}
#pragma mark -
#pragma UIActionSheet Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{NSLog(@"buttonIndex = [%d]",buttonIndex);switch (buttonIndex) {case 0://照相机{UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];imagePicker.delegate = self;imagePicker.allowsEditing = YES;imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
//            [self presentModalViewController:imagePicker animated:YES];[self presentViewController:imagePicker animated:YES completion:nil];}break;case 1://摄像机{UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];imagePicker.delegate = self;imagePicker.allowsEditing = YES;imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;imagePicker.videoQuality = UIImagePickerControllerQualityTypeLow;
//            [self presentModalViewController:imagePicker animated:YES];[self presentViewController:imagePicker animated:YES completion:nil];}break;case 2://本地相簿{UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];imagePicker.delegate = self;imagePicker.allowsEditing = YES;imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//            [self presentModalViewController:imagePicker animated:YES];[self presentViewController:imagePicker animated:YES completion:nil];}break;case 3://本地视频{UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];imagePicker.delegate = self;imagePicker.allowsEditing = YES;imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//            [self presentModalViewController:imagePicker animated:YES];[self presentViewController:imagePicker animated:YES completion:nil];}break;default:break;}
}#pragma mark -
#pragma UIImagePickerController Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(__bridge NSString *)kUTTypeImage]) {UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];[self performSelector:@selector(saveImage:)  withObject:img afterDelay:0.5];}else if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(__bridge NSString *)kUTTypeMovie]) {NSString *videoPath = [[info objectForKey:UIImagePickerControllerMediaURL] path];self.fileData = [NSData dataWithContentsOfFile:videoPath];}
//    [picker dismissModalViewControllerAnimated:YES];[picker dismissViewControllerAnimated:YES completion:nil];
}- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
//    [picker dismissModalViewControllerAnimated:YES];[picker dismissViewControllerAnimated:YES completion:nil];
}- (void)saveImage:(UIImage *)image {//    NSLog(@"保存头像!");//    [userPhotoButton setImage:image forState:UIControlStateNormal];BOOL success;NSFileManager *fileManager = [NSFileManager defaultManager];NSError *error;NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0];NSString *imageFilePath = [documentsDirectory stringByAppendingPathComponent:@"selfPhoto.jpg"];NSLog(@"imageFile->>%@",imageFilePath);success = [fileManager fileExistsAtPath:imageFilePath];if(success) {success = [fileManager removeItemAtPath:imageFilePath error:&error];}
//    UIImage *smallImage=[self scaleFromImage:image toSize:CGSizeMake(80.0f, 80.0f)];//将图片尺寸改为80*80UIImage *smallImage = [self thumbnailWithImageWithoutScale:image size:CGSizeMake(93, 93)];[UIImageJPEGRepresentation(smallImage, 1.0f) writeToFile:imageFilePath atomically:YES];//写入文件UIImage *selfPhoto = [UIImage imageWithContentsOfFile:imageFilePath];//读取图片文件
//    [userPhotoButton setImage:selfPhoto forState:UIControlStateNormal];self.img.image = selfPhoto;
}// 改变图像的尺寸,方便上传服务器
- (UIImage *) scaleFromImage: (UIImage *) image toSize: (CGSize) size
{UIGraphicsBeginImageContext(size);[image drawInRect:CGRectMake(0, 0, size.width, size.height)];UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return newImage;
}

2.保持原始图片的长宽比,生成需要尺寸的图片

//2.保持原来的长宽比,生成一个缩略图
- (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize
{UIImage *newimage;if (nil == image) {newimage = nil;}else{CGSize oldsize = image.size;CGRect rect;if (asize.width/asize.height > oldsize.width/oldsize.height) {rect.size.width = asize.height*oldsize.width/oldsize.height;rect.size.height = asize.height;rect.origin.x = (asize.width - rect.size.width)/2;rect.origin.y = 0;}else{rect.size.width = asize.width;rect.size.height = asize.width*oldsize.height/oldsize.width;rect.origin.x = 0;rect.origin.y = (asize.height - rect.size.height)/2;}UIGraphicsBeginImageContext(asize);CGContextRef context = UIGraphicsGetCurrentContext();CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);UIRectFill(CGRectMake(0, 0, asize.width, asize.height));//clear background[image drawInRect:rect];newimage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();}return newimage;
}

3.显示圆形头像

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0];NSString *imageFilePath = [documentsDirectory stringByAppendingPathComponent:@"selfPhoto.jpg"];NSLog(@"imageFile->>%@",imageFilePath);UIImage *selfPhoto = [UIImage imageWithContentsOfFile:imageFilePath];//self.img.image = selfPhoto;[self.img.layer setCornerRadius:CGRectGetHeight([self.img bounds]) / 2];  //修改半径,实现头像的圆形化self.img.layer.masksToBounds = YES;

示例包下载:TestImageController.rar

iOS---头像选取(照相或者图片库)、大小等比缩放、生成圆形头像相关推荐

  1. android生成圆形头像

    /*各种各样的图如何如何生成圆形头像,请下面看下面,可以先用bitmp直接用 Bitmap.createScaledBitmap(),如果是存到数据库里面的话,先把转它转成string,然后放取出来, ...

  2. php gd库 圆形头像,基于 GD 库生成圆形头像

    基于 GD 库生成圆形头像 laravel 友好的图片处理库 Intervention Image 可以绘制圆形(circle()), 但是好像并不能将即存的图片裁剪成圆形, 这里提供一个基于 GD ...

  3. php生成固定大小缩略图变形,php按指定大小等比缩放生成上传图片缩略图_PHP教程...

    php教程按指定大小等比缩放生成上传图片缩略图 /** * * *等比缩放 * @param unknown_type $srcImage 源图片路径 * @param unknown_type $t ...

  4. php生成固定大小缩略图变形,php实现按指定大小等比缩放生成上传图片缩略图的方法_PHP...

    本文实例讲述了php实现按指定大小等比缩放生成上传图片缩略图的方法.分享给大家供大家参考.具体实现方法如下: 代码如下: /** * * *等比缩放 * @param unknown_type $sr ...

  5. iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像

    <span class="comment">//弹出actionsheet.选择获取头像的方式</span> <span class="co ...

  6. 微信小程序海报画布生成圆形头像

    由于海报需求将用户头像在海报上呈圆形 实现过程如下 // 绘制圆形头像 //绘制的头像宽度let avatarurl_width = 40 //绘制的头像高度let avatarurl_heigth ...

  7. python和本人很像的卡通头像_用Python做一个网站,照片生成漫画头像,有这个网站就够了。...

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理 以下文章来源于腾讯云 作者:Python进阶者 对于动漫,大家一定都不陌生,小编周 ...

  8. java 海报二: 如何生成圆形头像

    直接上代码 //测试头像 String url = "https://thirdwx.qlogo.cn/mmopen/vi_32/ibwibmjniabu5JFwMl1Ju5drHnibjm ...

  9. itchat 保存好友信息以及生成好友头像图片墙

    2019 第 41 篇,总第 65 篇文章 本文大约 4000 字,阅读大约需要 12 分钟 最近简单运用 itchat 这个库来实现一些简单的应用,主要包括以下几个应用: 统计保存好友的数量和信息 ...

最新文章

  1. shell编程系列7--shell中常用的工具find、locate、which、whereis
  2. mysql profiling表_Mysql-性能分析(profiling 工具)
  3. Vue(五)Vue规范
  4. 卡巴斯基安全浏览器_360安全DNS正式推出DoH安全解析服务,打造安全上网“金钟罩”...
  5. 迭代加深搜索 C++解题报告 :[SCOI2005]骑士精神
  6. linux设备文件生成,Linux设备文件自动生成(示例代码)
  7. linux典型压缩包操作 tar打包、压缩与解压
  8. 如何画一个算法流图?
  9. 计算机上的刷新快捷键,刷新快捷键是
  10. mysql compute_compute by 的使用
  11. python NLP中文近义词
  12. SLAM notes
  13. 什么是服务器、云服务的优缺点是什么、为什么要使用云服务器?
  14. win10分区工具下载
  15. 单频点单输入功率只含基波X模型的提取与验证
  16. 计算机开机没有显示是什么原因是什么情况,电脑开机后显示器没有反应解决方法...
  17. [逻辑]-- 25匹马赛跑问题
  18. 30岁的程序员......
  19. maven同一个项目中,一个子模块引用另一个子模块的类的方法
  20. QuickBMS通用解包器使用指南

热门文章

  1. 计算机多媒体从时效分,多媒体技术基础大学计算机基础课件.ppt
  2. 简单在线编辑器的使用
  3. 第一篇:如何在Win10安装iis
  4. GIS地图在房地产中的应用
  5. java文件传输(JAVA文件传输的好处)
  6. 【自学宝典】自动化课程 / 西门子、三菱、欧姆龙PLC电气设计与编程
  7. 关于openCV报错无法打开文件“opencv_world340d.obj”的配置问题
  8. 麒麟V10离线安装VNC
  9. python 递增递减数列
  10. PlecsMMA学习2