ios 头像处理
来源:互联网 发布:前台怎么接收json 编辑:程序博客网 时间:2024/05/16 06:42
//
// HJDetilsViewController.m
// MusicLove
//
// Created by niuxinghua on 15/1/21.
// Copyright (c) 2015年 Hjojo. All rights reserved.
//
#import "HJDetilsViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import <QuartzCore/QuartzCore.h>
@interface HJDetilsViewController ()
@end
@implementation HJDetilsViewController
- (void)viewDidLoad {
[superviewDidLoad];
CGFloat mainwith=[UIScreenmainScreen].bounds.size.width;
CGFloat mainheight=[UIScreenmainScreen].bounds.size.height;
// Do any additional setup after loading the view.
// self.view.backgroundColor=[UIColor yellowColor];
self.navigationItem.title=@"个人资料";
UILabel* lable=[[UILabelalloc]initWithFrame:CGRectMake(5,64, mainwith/4, mainheight/8)];
[lable setText:@"头像"];
[lable setTextColor:[UIColorblackColor]];
[self.viewaddSubview:lable];
NSFileManager *fileManager = [NSFileManagerdefaultManager];
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [pathsobjectAtIndex:0];
NSString *imageFilePath = [documentsDirectorystringByAppendingPathComponent:@"avatar.jpg"];
_avatar=[[UIButtonalloc]initWithFrame:CGRectMake(mainwith/4*3,64, mainwith/4, mainwith/8)];
[_avataraddTarget:selfaction:@selector(pickImage)forControlEvents:UIControlEventTouchUpInside];
if(![fileManagerfileExistsAtPath:imageFilePath]) {
[_avatarsetBackgroundColor:[UIColorblackColor]];
}
else{
[_avatarsetBackgroundImage:[UIImageimageWithContentsOfFile:imageFilePath]forState:UIControlStateNormal];
}
[self.viewaddSubview:_avatar];
}
-(void)pickImage{
UIActionSheet* actionSheet = [[UIActionSheetalloc]
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) {
case0://照相机
{
UIImagePickerController *imagePicker = [[UIImagePickerControlleralloc] init];
imagePicker.delegate =self;
imagePicker.allowsEditing =YES;
imagePicker.sourceType =UIImagePickerControllerSourceTypeCamera;
// [self presentModalViewController:imagePicker animated:YES];
[selfpresentViewController:imagePickeranimated:YEScompletion:nil];
}
break;
case1://本地相簿
{
UIImagePickerController *imagePicker = [[UIImagePickerControlleralloc] init];
imagePicker.delegate =self;
imagePicker.allowsEditing =YES;
imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
// [self presentModalViewController:imagePicker animated:YES];
[selfpresentViewController:imagePickeranimated:YEScompletion:nil];
}
break;
default:
break;
}
}
#pragma mark -
#pragma UIImagePickerController Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if ([[infoobjectForKey:UIImagePickerControllerMediaType]isEqualToString:(__bridgeNSString *)kUTTypeImage]) {
UIImage *img = [infoobjectForKey:UIImagePickerControllerEditedImage];
[selfperformSelector:@selector(saveImage:) withObject:img afterDelay:0.5];
}
[picker dismissViewControllerAnimated:YEScompletion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
// [picker dismissModalViewControllerAnimated:YES];
[picker dismissViewControllerAnimated:YEScompletion:nil];
}
- (void)saveImage:(UIImage *)image {
// NSLog(@"保存头像!");
// [userPhotoButton setImage:image forState:UIControlStateNormal];
BOOL success;
NSFileManager *fileManager = [NSFileManagerdefaultManager];
NSError *error;
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [pathsobjectAtIndex:0];
NSString *imageFilePath = [documentsDirectorystringByAppendingPathComponent:@"avatar.jpg"];
NSLog(@"imageFile->>%@",imageFilePath);
success = [fileManager fileExistsAtPath:imageFilePath];
if(success) {
success = [fileManager removeItemAtPath:imageFilePatherror:&error];
}
// UIImage *smallImage=[self scaleFromImage:image toSize:CGSizeMake(80.0f, 80.0f)];//将图片尺寸改为80*80
UIImage *smallImage = [selfthumbnailWithImageWithoutScale:imagesize:CGSizeMake(93,93)];
[UIImageJPEGRepresentation(smallImage,1.0f) writeToFile:imageFilePathatomically:YES];//写入文件
UIImage *selfPhoto = [UIImageimageWithContentsOfFile:imageFilePath];//读取图片文件
// [userPhotoButton setImage:selfPhoto forState:UIControlStateNormal];
[_avatarsetBackgroundImage: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;
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//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, [[UIColorclearColor] CGColor]);
UIRectFill(CGRectMake(0,0, asize.width, asize.height));//clear background
[image drawInRect:rect];
newimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return newimage;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
- ios 头像处理
- iOS 上传头像处理
- ios 头像上传前的头像处理
- iOS中类似QQ圆形头像 处理
- iOS 开发APP更换用户头像问题的处理方式
- ios 图片处理-专辑封面头像旋转-裁剪圆形图片
- 上传头像的处理
- 圆形头像处理
- ios 头像上传
- IOS 圆形头像
- iOS 画圆形头像
- ios异步加载头像
- ios 制作方形头像
- iOS 设计圆形头像
- iOS实现圆形头像
- iOS 上传头像
- iOS实现圆形头像
- iOS实现圆形头像
- TortoiseSVN update时出现is not valid as filename错误
- 第十三章 异步和数据驱动编程
- unity自带LightMapping和3Ds Max烘焙贴图的小对比
- 更新交货单捡配数量
- MyEclipse10.1导出war包报错问题
- ios 头像处理
- bonobo server 自定义
- UVa 10700 Camel trading(计算式最大和最小值)
- css 自适应宽度
- Linux编译内核的详细配置
- SpringMVC 文件上传
- eclipse使用技巧1——优化调试代码
- sscanf,sscanf_s及其相关用法
- JQuery中$.ajax()方法参数详解