Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- /*
- info.plistに下の項目を追加する
- Privacy - Photo Library Usage Description
- Privacy - Camera Usage Description
- それぞれカメラとライブラリを使う理由を書く
- 「プロフィールに使うために使用します」など
- */
- class SettingPageController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
- override func viewDidLoad() {
- super.viewDidLoad()
- let userImageSize: Int = 100
- let userImageView: UIImageView = UIImageView()
- userImageView.frame = CGRect(x: 0, y: 20, width: userImageSize, height: userImageSize)
- userImageView.image = UIImage(named: "適当な画像")
- userImageView.center.x = self.view.frame.width/2
- userImageView.layer.cornerRadius = CGFloat(userImageSize/2)
- userImageView.clipsToBounds = true
- self.view.addSubview(userImageView)
- let userImageViewButton: UIButton = UIButton()
- userImageViewButton.frame = CGRect(x: 0, y: 20, width: userImageSize, height: userImageSize)
- userImageViewButton.center.x = self.view.frame.width/2
- userImageViewButton.layer.cornerRadius = CGFloat(userImageSize/2)
- userImageViewButton.clipsToBounds = true
- userImageViewButton.addTarget(self, action: #selector(self.onClickUserPlofileButton(sender:)), for: .touchUpInside)
- self.view.addSubview(userImageViewButton)
- }
- /**
- 画像編集するボタン
- - Parameters: sender: UIButton
- */
- internal func onClickUserPlofileButton(sender: UIButton) {
- showAlert()
- }
- func showCameraLibrary(){
- if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
- let picker = UIImagePickerController()
- picker.modalPresentationStyle = UIModalPresentationStyle.fullScreen
- picker.delegate = self
- picker.sourceType = UIImagePickerControllerSourceType.camera
- self.present(picker, animated: true, completion: nil)
- }
- }
- func showPhotoLibrary() {
- if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
- let picker = UIImagePickerController()
- picker.modalPresentationStyle = UIModalPresentationStyle.popover
- picker.delegate = self
- picker.sourceType = UIImagePickerControllerSourceType.photoLibrary
- if let popover = picker.popoverPresentationController {
- popover.sourceView = self.view
- popover.sourceRect = self.view.frame
- popover.permittedArrowDirections = UIPopoverArrowDirection.any
- }
- self.present(picker, animated: true, completion: nil)
- }
- }
- func showAlert() {
- let alert: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
- let defaultAction1: UIAlertAction = UIAlertAction(title: "写真の選択", style: UIAlertActionStyle.default, handler:{
- (action: UIAlertAction!) -> Void in
- self.showPhotoLibrary()
- })
- let defaultAction2: UIAlertAction = UIAlertAction(title: "カメラで撮影", style: UIAlertActionStyle.default, handler:{
- (action: UIAlertAction!) -> Void in
- self.showCameraLibrary()
- })
- let defaultAction3: UIAlertAction = UIAlertAction(title: "キャンセル", style: UIAlertActionStyle.cancel, handler:{
- (action: UIAlertAction!) -> Void in
- })
- alert.addAction(defaultAction1)
- alert.addAction(defaultAction2)
- alert.addAction(defaultAction3)
- present(alert, animated: true, completion: nil)
- }
- func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
- if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
- print(pickedImage.size)
- uploadFile(image: pickedImage)
- }
- picker.dismiss(animated: true, completion: nil)
- }
- func uploadFile(image: UIImage){
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement