Skip to content

Instantly share code, notes, and snippets.

@gitbricho
Created February 4, 2016 14:45
Show Gist options
  • Save gitbricho/1fdd9eb1e6489e3c1250 to your computer and use it in GitHub Desktop.
Save gitbricho/1fdd9eb1e6489e3c1250 to your computer and use it in GitHub Desktop.
IosImageView
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate , UINavigationControllerDelegate {
// Mark:プロパティ
@IBOutlet weak var photoImageVIew: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Mark:アクション
@IBAction func selectImage(sender: UITapGestureRecognizer) {
// フォトライブラリからメディアを取得
let imagePickerController = UIImagePickerController()
// 写真だけが取得できるるようにする
imagePickerController.sourceType = .PhotoLibrary
// ユーザーのイメージ・ピックを監視
imagePickerController.delegate = self
//ピッカーを表示
presentViewController(imagePickerController, animated: true, completion: nil)
}
func imagePickerController(picker:UIImagePickerController,
didFinishPickingMediaWithInfo info:[String:AnyObject]) {
// info ディクショナリは複数のイメージを持つ。以下はオリジナル画像を使用
let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
// 自動レイアウト解除
//photoImageVIew.translatesAutoresizingMaskIntoConstraints = true
// イメージビューをセンターに配置
//photoImageVIew.center = CGPointMake(self.view.bounds.width / 2, self.view.bounds.height / 2);
// 表示モードの設定
//photoImageVIew.contentMode = UIViewContentMode.ScaleAspectFit
// 選択イメージを表示するため、photoImageView に代入
photoImageVIew.image = selectedImage
// ピッカーを隠す
dismissViewControllerAnimated(true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment