iOS 從相機或相簿擷取圖片並裁剪

來源:互聯網
上載者:User

   今天遇到一個帳戶圖片上傳的問題,需要從相簿或者相機中讀取圖片。代碼很簡單,抽取關鍵區段,如下:


 

/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,萬事搞定。


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.