Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

[Swift] 画像を合成する

公開日: : iPhone App 開発, Swift


Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

画像を合成

以下の2つの画像を合成してみます。円と四角です。

square
circle

画像をプロジェクトに追加

画像をプロジェクトに追加します。
プロジェクト名を右クリック、「Add Files to …」で画像ファイルを選択します。
スクリーンショット 2014-12-02 15.02.18
プロジェクト->TARGETSのプロジェクトを選択し、「Build Phases」タブの「Copy Bundle Resources」を参照し、画像ファイルが追加されたか確認します。
スクリーンショット_2014-12-02_15_49_42

StoryBoard

StoryBoardにUIImageViewを追加してください。
サイズは画像サイズに合わせること。
そしてViewControllerにUIImageViewを結びつけます。(outlet)
スクリーンショット_2014-12-02_14_35_18_png

ViewController.swift

メインのViewControllerのソースです。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        imageView.image = self.synthesizeImage(["square", "circle"], size: CGSize(width: 70, height: 70))
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func synthesizeImage(names: Array<String>, size: CGSize) -> UIImage {
        
        UIGraphicsBeginImageContextWithOptions(size, false, 0.0);
        for name in names {
            if let image = UIImage(named: name) {
                image.drawInRect(CGRectMake(0, 0, image.size.width, image.size.height))
            }
        }
        let newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage
    }
}

画像の合成は16行目のsynthesizeImage関数で処理します。
引数はStringの配列と、CGSizeです。
Stringには画像ファイルの名前を格納、CGSizeは画像サイズを渡します。
引数は好きなようにカスタマイズしてください。画像ファイルが固定ならベタで書いてもいいかも。

19行目のfor文で配列の画像を繰り返し処理します。
9行目で出力。

結果

iOS Simulator Screen Shot 2014.12.02 16.00.45

ad

関連記事

[Swift] スクリーンサイズを取得する

スクリーンサイズを取得 iPhoneの幅と高さを取得する関数を紹介します。 iOS8から画面の傾

記事を読む

[Objective-C] iPhoneアプリからのメーラー起動

今回はiPhoneアプリからメーラーを起動する処理を紹介します。 メーラー起動のコントローラー

記事を読む

[Swift] optional値の設定(??)

optional値がnilの時 プロジェクトでのテンプレートを選択した時にソースコードが自動生成さ

記事を読む

[Swift] UITabBarControllerでタブ選択時のイベントを取得する

UITabBarController タブを使った画面を作成する場合はUITabBarContro

記事を読む

[Objective-C] 共通関数をまとめたクラスを作ろう!

共通クラスとは? アプリを作っていると「この処理何回も書いてるなー」ということが多々あると思います

記事を読む

[Swift] UINavigationControllerで戻るイベントを取得する

UINavigationController UINavigationControllerを使用し

記事を読む

no image

一旦、離脱中!

サイト制作の仕事がちょろちょろちょろっと入ってきたので、寂しいけれどしばしXcodeとはお別れ。

記事を読む

[Swift] プロパティリスト(plist)の値を取得

plistからデータを取得してみます。 こちらのエントリーも参考にしてみてください。 プロパテ

記事を読む

[Swift] プロパティリスト(plist)を使ってCoreDataで一括登録する

プロパティリストを使う 初期データやあらかじめ用意したデータを一括登録したい場合、プロパティリスト

記事を読む

no image

小さなことからコツコツと

これまでHTMLとCSSだけで生きてきただけあって、プログラム言語がいまいちわからない。 Mo

記事を読む

ad

Message

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

ad

[PHP] curl転送してみる(googleに)

curlでgoogle画像検索APIにアクセスしてみます。 cu

[PHP] PDOでMySQLの接続確認をする

PDO PHPでMySQLに接続する際には、mysql_connec

[PHP] ランダムな英数字を生成する

便利系メソッド 今回はPHPでランダムな英数字を作成してみます。

[Swift] プロパティリスト(plist)の値を取得

plistからデータを取得してみます。 こちらのエントリーも参考にし

[Swift] Asset Catalogについて

XCode5から追加されたAsset Catalog。 いままで標準

→もっと見る

  • 1978年の七夕生まれ。 25才でweb業界の門を叩き、28才でフリーランスに。 現在は、フリーランスでマークアップ中心に、wordpressのカスタマイズやデザインをしております。 また、iPhoneアプリの開発もしております。

Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524
PAGE TOP ↑

Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524