//弹出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:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"照相机",@"摄像机",@"本地相簿",@"本地视频",nil];
[actionSheet showInView:self.view]; }
#pragma mark -
#pragma UIActionSheet Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"buttonIndex = [%d]",buttonIndex);
switch (buttonIndex) {
case ://照相机
{
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 ://摄像机
{
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 ://本地相簿
{
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 ://本地视频
{
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:];
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*80
UIImage *smallImage = [self thumbnailWithImageWithoutScale:image size:CGSizeMake(, )];
[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(, , 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)/;
rect.origin.y = ;
}
else{
rect.size.width = asize.width;
rect.size.height = asize.width*oldsize.height/oldsize.width;
rect.origin.x = ;
rect.origin.y = (asize.height - rect.size.height)/;
}
UIGraphicsBeginImageContext(asize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
UIRectFill(CGRectMake(, , 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:];
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]) / ]; //修改半径,实现头像的圆形化
self.img.layer.masksToBounds = YES;

希望能对大家有帮助,如果你有更好的实现方式。欢迎告诉我。

demo下载地址:http://download.csdn.net/detail/wangtao169447/7515487

https://github.com/wangtao169447/TakeAvatarPhoto

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

  1. Quartz2D之生成圆形头像、打水印、截图三种方法的封装

    我给UIImage类添加了一个类目,用于封装三个方法,每个方法都没有难度,做这个主要为了练习一下封装: 首先在类目.h文件中声明三个方法:以及创建了一个枚举.用于水印方法中设定水印位置:方法说明和参数 ...

  2. 【Android】自己定义圆形ImageView(圆形头像 可指定大小)

    近期在仿手Q的UI,这里面常常要用到的就是圆形头像,看到 在android中画圆形图片的几种办法 这篇文章,了解了制作这样的头像的原理.只是里面提供的方法另一个不足的地方就是不能依据实际需求改变图片的 ...

  3. iOS 画圆形头像

    demo下载地址:http://pan.baidu.com/s/1mgBf6YG _logoImageView.image = [self getEllipseImageWithImage:[UIIm ...

  4. Android设置头像,手机拍照或从本地相冊选取图片作为头像

     [Android设置头像,手机拍照或从本地相冊选取图片作为头像] 像微信.QQ.微博等社交类的APP,通常都有设置头像的功能,设置头像通常有两种方式: 1,让用户通过选择本地相冊之类的图片库中已 ...

  5. Android ImageView圆形头像

    转载自:http://m.oschina.net/blog/321024 Android ImageView圆形头像 图片完全解析 我们在做项目的时候会用到圆形的图片,比如用户头像,类似QQ.用户在用 ...

  6. Duilib实现圆形头像控件

    .h文件 #ifndef __UIHEADICON_H__ #define __UIHEADICON_H__ /* 名称:圆形头像控件(派生CButtonUI类) */ class CHeadUI: ...

  7. UWP xaml 圆形头像

    圆形头像 去掉黑边 拖动打开图形 圆形头像 现在很多软件都喜欢使用圆形头像 win10 uwp使用圆形头像很简单 <Ellipse Width="200" Height=&q ...

  8. WordPress制作圆形头像友情链接页面的方法

    网上看见过很多种友情链接页面,我比较喜欢的是圆形头像的这种,先看看效果吧:传送门 就是这种上面是圆形的友链用户头像,下面是友链用户网站名,然后鼠标移上去头像会旋转,怎么实现这种效果呢?我在网上找了很多 ...

  9. Android自定义控件实例,圆形头像(图库 + 裁剪+设置),上传头像显示为圆形,附源码

    Android项目开发中经常会遇见需要实现圆角或者圆形的图片功能,如果仅仅使用系统自带的ImageView控件显然无法实现此功能,所以通过系列文章的形式由简到繁全方位的介绍一下此功能的实现,巩固一下自 ...

随机推荐

  1. CentOS下安装配置SVN服务器并自动同步到web目录

    一.安装 yum install subversion测试是否安装成功 /usr/bin/svnserve --version如提示以下内容,说明已安装成功 svnserve,版本 1.6.11 (r ...

  2. 一看你就懂,超详细java中的ClassLoader详解(转)

    转载地址     http://blog.csdn.net/briblue/article/details/54973413   目录(?)[-] Class文件的认识 你还记得java环境变量吗 J ...

  3. centos7安装配置时间服务器

    前言: 时间服务器是S/C模型服务,需要配置服务端和客户端 NTP服务端配置:(服务端的IP为1.1.1.14)安装ntp服务:# yum -y install ntp查询网络中的NTP服务器:# n ...

  4. hadoop HDFS文件系统的特征

    hadoop HDFS文件系统的特征 存储极大数目的信息(terabytes or petabytes),将数据保存到大量的节点当中.支持很大单个文件. 提供数据的高可靠性,单个或者多个节点不工作,对 ...

  5. 线段树 SP1716 GSS3 - Can you answer these queries III

    SP1716 GSS3 - Can you answer these queries III 题意翻译 n 个数,q 次操作 操作0 x y把A_xAx 修改为yy 操作1 l r询问区间[l, r] ...

  6. django内置服务器

    单进程多线程的 多线程用来并发,各个线程之间不会阻塞,每个线程对应一个连接

  7. nginx 搭建 rtmp 服务器

    前言 最近接手了一个跟视频监控相关的项目,用了近年来越来越流行的 Web 服务器 nginx 加上 nginx-rtmp-module 搭建 rtmp 服务器.使用了阿里云的服务器,系统 Ubuntu ...

  8. [Maven]How do I tell Maven to use the latest version of a dependency?

    Link: http://stackoverflow.com/questions/30571/how-do-i-tell-maven-to-use-the-latest-version-of-a-de ...

  9. Django之ORM其他骚操作 执行原生SQl

      Django ORM执行原生SQL # extra # 在QuerySet的基础上继续执行子语句 # extra(self, select=None, where=None, params=Non ...

  10. dataTable调用接口渲染数据,没有数据,报错

    当没有数据的时候,报错: 解决方法: 在后台那边处理一下,当没有数据的时候,令 data : ' ' 或者 data : [ ] 前端代码: var loading = layer.load(1, { ...