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: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 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*80

UIImage *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.保持原来的长宽比,生成一个缩略图

- (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;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,362评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,330评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,247评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,560评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,580评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,569评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,929评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,587评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,840评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,596评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,678评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,366评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,945评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,929评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,165评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,271评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,403评论 2 342

推荐阅读更多精彩内容