iOS從相機或相冊獲取圖片並裁剪,再獲取裁剪後的圖片

-(void) selectWayToGetPicture{
  UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"圖庫", nil];
  [actionSheet showInView:self.view];
}

// 實現UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate

#pragma mark -actionSheetDelegate
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
  if (buttonIndex == 0) {
    // 資源類型爲照相機
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    // 判斷是否有相機
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){
      UIImagePickerController *picker = [[UIImagePickerController alloc] init];
      picker.delegate = self;
      picker.sourceType = sourceType; // 資源類型爲照相機
      picker.allowsEditing = YES;     // 設置選擇後的圖片是否能被編輯
      [self presentViewController:picker animated:YES completion:nil];
    }else {
      UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"該設備無攝像頭"delegate:self cancelButtonTitle:@"取消"otherButtonTitles: nil];
      [alertView show];
    }
  }else if(buttonIndex == 1){
    UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
    pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    pickerController.delegate = self;
    pickerController.allowsEditing = YES; // 設置選擇後的圖片是否能被編輯
    [self presentViewController:pickerController animated:YES completion:nil];
  }
}

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
  // 當選擇的類型是圖片
  if ([type isEqualToString:@"public.image"])
  {
    UIImage* image = [info objectForKey:@"UIImagePickerControllerEditedImage"]; // 裁剪後的圖片
  }
  [picker dismissViewControllerAnimated:YES completion:nil];
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章