您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 從相機或相冊取圖並裁剪為正方形頭像

編輯:iOS 開發綜合

做用戶頭像上傳時,需要從相冊或相機讀取圖片。流程本身不複雜,但細節不少,下面把關鍵部分抽出來講。

弹出選擇面板

- (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 - UIActionSheetDelegate

- (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 - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{}];

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    userImageView.image = image;

    NSData *imageData = UIImageJPEGRepresentation(image, COMPRESSED_RATE);
    UIImage *compressedImage = [UIImage imageWithData:imageData];
    [HttpRequestManager uploadImage:compressedImage httpClient:self.httpClient delegate:self];
}

為什麼圖片會被拉伸

頭像框通常是正方形,而相冊裡的原始照片比例各異,直接塞進去就會明顯變形。折騰了一堆裁剪算法、甚至準備自定義一個裁剪視圖之後,才注意到取圖時用的是這個常量:

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 asset

既然設置了 allowsEditing = YES,系統已經讓用戶完成過一次裁剪,那麼直接改用 UIImagePickerControllerEditedImage 就能拿到裁好的方圖,問題迎刃而解。同時還可以用 UIImagePickerControllerCropRect 讀回裁剪區域,需要記錄用戶裁切位置時非常方便。

補充提醒

需要注意的是,UIImagePickerController 自帶的編輯界面只能裁成固定比例,可定制性有限。若設計要求自定義馬克、圓形遮罩或多種比例,就該轉向 PhotosUI 提供的 PHPickerViewController 配合自寫裁剪界面。另外,在真實項目中還應處理 info 中 UIImagePickerControllerImageURL 缺失的情況,並引導用戶在系統設置中開啟相冊權限。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved