今天遇到一個用戶頭像上傳的問題,需要從相冊或者相機中讀取圖片。代碼很簡單,抽取關鍵部分,如下:
/load user image - (void)UesrImageClicked { UIActionSheet *sheet; // 判斷是否支持相機 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { sheet = [[UIActionSheet alloc] initWithTitle:@"選擇圖像" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照", @"從相冊選擇", nil]; } else { sheet = [[UIActionSheet alloc] initWithTitle:@"選擇圖像" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"從相冊選擇", nil]; } sheet.tag = 255; [sheet showInView:self.view]; } #pragma mark - action sheet delegte - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (actionSheet.tag == 255) { NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // 判斷是否支持相機 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { switch (buttonIndex) { case 0: return; case 1: //相機 sourceType = UIImagePickerControllerSourceTypeCamera; break; case 2: //相冊 sourceType = UIImagePickerControllerSourceTypePhotoLibrary; break; } } else { if (buttonIndex == 0) { return; } else { sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; } } // 跳轉到相機或相冊頁面 UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES; imagePickerController.sourceType = sourceType; [self presentViewController:imagePickerController animated:YES completion:^{}]; } } #pragma mark - image picker delegte - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissViewControllerAnimated:YES completion:^{}]; UIImage *image = [info UIImagePickerControllerOriginalImage]; userImageView.image = image; NSData *imageData = UIImageJPEGRepresentation(image, COMPRESSED_RATE); UIImage *compressedImage = [UIImage imageWithData:imageData]; [HttpRequestManager uploadImage:compressedImage httpClient:self.httpClient delegate:self]; } //load user image - (void)UesrImageClicked { UIActionSheet *sheet; // 判斷是否支持相機 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { sheet = [[UIActionSheet alloc] initWithTitle:@"選擇圖像" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照", @"從相冊選擇", nil]; } else { sheet = [[UIActionSheet alloc] initWithTitle:@"選擇圖像" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"從相冊選擇", nil]; } sheet.tag = 255; [sheet showInView:self.view]; } #pragma mark - action sheet delegte - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (actionSheet.tag == 255) { NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // 判斷是否支持相機 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { switch (buttonIndex) { case 0: return; case 1: //相機 sourceType = UIImagePickerControllerSourceTypeCamera; break; case 2: //相冊 sourceType = UIImagePickerControllerSourceTypePhotoLibrary; break; } } else { if (buttonIndex == 0) { return; } else { sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; } } // 跳轉到相機或相冊頁面 UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES; imagePickerController.sourceType = sourceType; [self presentViewController:imagePickerController animated:YES completion:^{}]; } } #pragma mark - image picker delegte - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissViewControllerAnimated:YES completion:^{}]; UIImage *image = [info UIImagePickerControllerOriginalImage]; userImageView.image = image; NSData *imageData = UIImageJPEGRepresentation(image, COMPRESSED_RATE); UIImage *compressedImage = [UIImage imageWithData:imageData]; [HttpRequestManager uploadImage:compressedImage httpClient:self.httpClient delegate:self]; }
創建一張表單,選擇相機或者相冊,然後用回調函數處理返回的圖片。
但是返回的圖像並不是正方形,顯示在用戶頭像的View裡面產生明顯的拉伸。研究了很久各種裁剪算法,甚至想創建一個view來處理。突然發現代碼中從相機、相冊獲取圖片時采用的的
UIImagePickerControllerOriginalImage
於是跟蹤進去一看:
UIKIT_EXTERN NSString *const UIImagePickerControllerMediaType; // an NSString (UTI, i.e. kUTTypeImage)
UIKIT_EXTERN NSString *const UIImagePickerControllerOriginalImage; // a UIImage
UIKIT_EXTERN NSString *const UIImagePickerControllerEditedImage; // a UIImage
UIKIT_EXTERN NSString *const UIImagePickerControllerCropRect; // an NSValue (CGRect)
UIKIT_EXTERN NSString *const UIImagePickerControllerMediaURL; // an NSURL
UIKIT_EXTERN NSString *const UIImagePickerControllerReferenceURL NS_AVAILABLE_IOS(4_1); // an NSURL that references an
恍然大悟,很簡單,用UIImagePickerControllerEditedImage,萬事搞定。