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] 関数型プログラミング

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

記事を読む

no image

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

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

記事を読む

iPhoneApp WorkManager公開されました!

きっとリジェクトされるんだろうなと思いながら申請を出して1週間。 本日、公開のメールが届きまし

記事を読む

no image

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

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

記事を読む

[iPhone App] WorkManager 1.2.0版がリリースされました。

新機能 以前から要望などが多かったものを元に、機能を追加いたしました。 新バージョンで追加された

記事を読む

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

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

記事を読む

no image

一旦、離脱中!

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

記事を読む

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

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

記事を読む

no image

迷走継続中!

どうも、僕です。 相変わらず迷走中な訳ですが、新たに迷走アイテムを入手しました。 その名

記事を読む

[Objective-C] バージョン更新時に変更内容をアラートで出力する

アプリの更新内容を表示したい! バージョン更新時に変更内容を表示させたいのは、ユーザーがいちいちA

記事を読む

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