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

2024-01-12 17:58

本文主要是介绍iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

//弹出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;
}

显示圆形头像
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;

这篇关于iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/598688

相关文章

Redis实现RBAC权限管理

《Redis实现RBAC权限管理》本文主要介绍了Redis实现RBAC权限管理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1. 什么是 RBAC?2. 为什么使用 Redis 实现 RBAC?3. 设计 RBAC 数据结构

SpringBoot基于沙箱环境实现支付宝支付教程

《SpringBoot基于沙箱环境实现支付宝支付教程》本文介绍了如何使用支付宝沙箱环境进行开发测试,包括沙箱环境的介绍、准备步骤、在SpringBoot项目中结合支付宝沙箱进行支付接口的实现与测试... 目录一、支付宝沙箱环境介绍二、沙箱环境准备2.1 注册入驻支付宝开放平台2.2 配置沙箱环境2.3 沙箱

Nginx实现高并发的项目实践

《Nginx实现高并发的项目实践》本文主要介绍了Nginx实现高并发的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录使用最新稳定版本的Nginx合理配置工作进程(workers)配置工作进程连接数(worker_co

python中列表list切分的实现

《python中列表list切分的实现》列表是Python中最常用的数据结构之一,经常需要对列表进行切分操作,本文主要介绍了python中列表list切分的实现,文中通过示例代码介绍的非常详细,对大家... 目录一、列表切片的基本用法1.1 基本切片操作1.2 切片的负索引1.3 切片的省略二、列表切分的高

基于Python实现一个PDF特殊字体提取工具

《基于Python实现一个PDF特殊字体提取工具》在PDF文档处理场景中,我们常常需要针对特定格式的文本内容进行提取分析,本文介绍的PDF特殊字体提取器是一款基于Python开发的桌面应用程序感兴趣的... 目录一、应用背景与功能概述二、技术架构与核心组件2.1 技术选型2.2 系统架构三、核心功能实现解析

使用Python实现表格字段智能去重

《使用Python实现表格字段智能去重》在数据分析和处理过程中,数据清洗是一个至关重要的步骤,其中字段去重是一个常见且关键的任务,下面我们看看如何使用Python进行表格字段智能去重吧... 目录一、引言二、数据重复问题的常见场景与影响三、python在数据清洗中的优势四、基于Python的表格字段智能去重

Spring AI集成DeepSeek实现流式输出的操作方法

《SpringAI集成DeepSeek实现流式输出的操作方法》本文介绍了如何在SpringBoot中使用Sse(Server-SentEvents)技术实现流式输出,后端使用SpringMVC中的S... 目录一、后端代码二、前端代码三、运行项目小天有话说题外话参考资料前面一篇文章我们实现了《Spring

Nginx中location实现多条件匹配的方法详解

《Nginx中location实现多条件匹配的方法详解》在Nginx中,location指令用于匹配请求的URI,虽然location本身是基于单一匹配规则的,但可以通过多种方式实现多个条件的匹配逻辑... 目录1. 概述2. 实现多条件匹配的方式2.1 使用多个 location 块2.2 使用正则表达式

使用Apache POI在Java中实现Excel单元格的合并

《使用ApachePOI在Java中实现Excel单元格的合并》在日常工作中,Excel是一个不可或缺的工具,尤其是在处理大量数据时,本文将介绍如何使用ApachePOI库在Java中实现Excel... 目录工具类介绍工具类代码调用示例依赖配置总结在日常工作中,Excel 是一个不可或缺的工http://

SpringBoot实现导出复杂对象到Excel文件

《SpringBoot实现导出复杂对象到Excel文件》这篇文章主要为大家详细介绍了如何使用Hutool和EasyExcel两种方式来实现在SpringBoot项目中导出复杂对象到Excel文件,需要... 在Spring Boot项目中导出复杂对象到Excel文件,可以利用Hutool或EasyExcel