国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

iOS實現頭像選取(照相或者圖片庫)、大小等比縮放、生成圓形頭像

2019-11-14 20:24:51
字體:
來源:轉載
供稿:網友
//彈出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.保持原始圖片的長寬比,生成需要尺寸的圖片

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

 

希望能對大家有幫助,如果你有更好的實現方式。歡迎告訴我。

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

https://github.com/wangtao169447/TakeAvatarPhoto


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌兰察布市| 平乡县| 陇南市| 洛阳市| 兰西县| 慈溪市| 吴江市| 合山市| 洮南市| 基隆市| 太仓市| 天全县| 林口县| 中卫市| 日土县| 健康| 长子县| 固始县| 宽甸| 普安县| 阳新县| 射阳县| 长岭县| 察隅县| 伊春市| 化州市| 常山县| 黎平县| 米林县| 左云县| 高安市| 察雅县| 万源市| 故城县| 南开区| 庄河市| 博乐市| 聂拉木县| 黑河市| 淮南市| 工布江达县|