iOS---头像选取(照相或者图片库)、大小等比缩放、生成圆形头像
2015-10-15 21:53
567 查看
转载自: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开发总结之代码规范(下)
- iOS开发总结之代码规范(上)
- iOS 警告收录及科学快速的消除方法
- iOS 播放视频
- iOS:核心动画的详解介绍:CAAnimation(抽象类)及其子类
- iOS 核心动画
- 我的IOS学习历程 - 第六天2
- 我的IOS学习历程 - 第六天1
- IOS 开发者账号 (team账号)
- 我的IOS学习历程-第五天
- IOS 通知传值
- IOS 8 基本定位实现
- flash发布ipa安装到ios8真机测试
- iOS中NSString,NSData和char的相互转换,JSON串转化为NSDictionary或NSArray
- IOS NSMutableAttributedString 富文本的设置
- iOS的主要框架介绍
- iOS开发总结之代码规范
- 一个layer可以跟着画完的线移动ios程序 好玩啊。
- 一个iOS bug引发的思考
- 开始iOS 7中自动布局教程(一)