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

関連記事

no image

OSSの使い方がわからない・・・。

チュートリアル系の、storyboradでview controllerを置いて画面遷移を設定してっ

記事を読む

Xcode5のstoryboard内でUIKeyCommandエラーの対処法

既存のプロジェクトからstoryboardにあるオブジェクトをコピペして、新規プロジェクトに貼付けた

記事を読む

[Objective-C] 文字列操作の便利Tips

Tipsいろいろ 先日の投稿 共通関数をまとめたクラスを作ろう!の流れで、文字列を操作するときに便

記事を読む

no image

爆走中!

挫折街道爆走中です! が! 迷走中でもあるわけです、はい。 そんな中で、迷走するに至った訳

記事を読む

[Objective-C] no architectures to compile for … のコンパイルエラーについて

起動したプロジェクトでコンパイルエラーが出ました。 no architectures to com

記事を読む

[Swift] 関数型プログラミング

関数型プログラミングについて、ストーリー仕立ての面白い記事がありましたので紹介します。 IQ1

記事を読む

[Swift] 定数クラスをつくってみる

Objective-cの場合 今回は共通にする定数をまとめるクラスを作ってみます。 まずはO

記事を読む

[Swift] Swiftでいろんなfor文まとめてみた

いろんなfor文 プログラム作成で必ず出てくるfor文を自分なりにまとめてみました。 Swift

記事を読む

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

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

記事を読む

App開発をはじめてみました。

はじめてみたはいいものの、右も左もわからず、書籍を読みあさり、ネットを徘徊し、いろんなことを調べ上げ

記事を読む

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