Help us understand the problem. What is going on with this article?

画像表示とそのクリッピング、セグメント操作など

More than 5 years have passed since last update.

アプリ画面

やったこと

  • プログラムから画像を表示する
  • 画像のクリッピングとか縦横比固定方法を確認する
  • セグメント(スライダーみたいなやつ)制御方法を確認する

コード

ViewController.swift
class ViewController: UIViewController {

    // 画像表示のイメージビュー
    @IBOutlet var myDisplay: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    // 画像を表示
    @IBAction func dispImage(sender: UIButton) {

        // ファイル名を指定
        var img:UIImage! = UIImage(named:"winter_present.png")
        // 座標とサイズを指定
        var rect:CGRect = CGRectMake(90, 80, 100, 100)
        var imageView = UIImageView()
        imageView.frame = rect
        imageView.image = img
        // 縦横比保持
        imageView.contentMode = UIViewContentMode.ScaleAspectFit
        // ビューに追加
        self.view.addSubview(imageView)


        // ファイル名を指定
        var img2:UIImage! = UIImage(named:"winter_present.png")
        // 座標とサイズを指定
        var rect2:CGRect = CGRectMake(90, 70, 200, 200)
        var imageView2 = UIImageView()
        imageView2.frame = rect2
        imageView2.image = img2
        // 伸縮しないで、左上を固定
        imageView2.contentMode = UIViewContentMode.TopLeft
        // rect2のサイズを超えたらカット
        imageView2.clipsToBounds = true
        // ビューに追加
        self.view.addSubview(imageView2)


    }
    // 画像2非表示にする(セグメント切り替え)
    @IBAction func changeSegment(sender: UISegmentedControl) {

        var fileName = "winter_iceskate.png"
        if (sender.selectedSegmentIndex == 1) {
            self.myDisplay.image = UIImage(named: fileName)
        } else {
            self.myDisplay.image = nil
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

ソース

https://github.com/hanoopy/ios_study_imageapp

コメント

  • ファイル名はグループ名(フォルダ名)を気にしなくて良い。
    ×Images/winter_present.png
    ○winter_present.png

  • やっとnilを使った

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした