app 头像设置,做成圆形的越来越多。而实现并不是很难,但是图像处理好很重要。
<span style="font-size:14px;">UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(200,100 , 100, 100)]; UIImage *image = [UIImage imageNamed:@"1"]; imgV.image = image; [imgV.layer setCornerRadius:CGRectGetHeight([imgV bounds])/2]; imgV.layer.masksToBounds = YES; [self.view addSubview: imgV];</span>
上面的代码是将 长宽都是100的imageView 以50为半径 切的一个圆。
另外一个是等比例拉伸的。功能实现是,从本地或者相册选择图片来做头像并作保存。
#import "ViewController.h" #import <MobileCoreServices/MobileCoreServices.h> @interface ViewController () @property(nonatomic, strong) UIImageView *img; @property(nonatomic, strong) NSData *fileData; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.img = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *imageFilePath = [documentsDirectory stringByAppendingPathComponent:@"selfPhoto.jpg"]; UIImage *selfPhoto = [UIImage imageWithContentsOfFile:imageFilePath]; self.img.image = selfPhoto; [self.img.layer setCornerRadius:CGRectGetHeight([self.img bounds]) / 2]; self.img.layer.masksToBounds = YES; [self.view addSubview:_img]; UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 300, 100, 50)]; [self.view addSubview:btn]; [btn setTitle:@"换图" forState:UIControlStateNormal]; // [btn addTarget:self action:@selector(takePictureClick) forControlEvents:UIControlEventTouchUpInside]; btn.backgroundColor = [UIColor lightGrayColor]; UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(200,100 , 100, 100)]; UIImage *image = [UIImage imageNamed:@"1"]; imgV.image = image; [imgV.layer setCornerRadius:CGRectGetHeight([imgV bounds])/2]; imgV.layer.masksToBounds = YES; [self.view addSubview: imgV]; } //从相册获取图片 -(void)takePictureClick { UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:@"请选择文件来源" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相机",@"本地相簿",nil]; [actionSheet showInView:self.view]; } - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)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 = UIImagePickerControllerSourceTypePhotoLibrary; // [self presentModalViewController:imagePicker animated:YES]; [self presentViewController:imagePicker animated:YES completion:nil]; } break; default: break; } } - (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)saveImage:(UIImage *)image { 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 thumbnailWithImageWithoutScale:image size:CGSizeMake(100, 100)]; [UIImageJPEGRepresentation(smallImage, 1.0f) writeToFile:imageFilePath atomically:YES]; //写入文件 UIImage *selfPhoto = [UIImage imageWithContentsOfFile:imageFilePath];//读取图片文件 self.img.image = selfPhoto; } //保持原来的长宽比,生成一个缩略图 - (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; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-16 20:30:01