iPhoneアプリ開発環境を整えるで作成したプロジェクトに次のicon.pngを追加し、Helloクラスを修正してicon.pngのイメージを表示します。
画像ファイルの追加方法
まず、画像ファイルをXcode4のプロジェクトに追加します。追加方法は、まずナビゲータエリア(下図の左側のエリア)に画像ファイルをドラッグ&ドロップします。すると[Choose options for adding these files:]の画面が現れるので、[Copy items into destination into destination group's folder]にチェックを入れて[Finish]ボタンを押下します。このチェックは画像ファイルをプロジェクトフォルダへコピーするというオプションです。
画像描画プログラム
次のようにHello.hとHello.mを修正しました。
Hello.h
#import <UIKit/UIKit.h>
@interface Hello : UIView {
UIImage* _image;
}
@end
Hello.m
#import "Hello.h"
#import "Information.h"
@implementation Hello
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor=[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
_image=[[UIImage imageNamed:@"icon.png"] retain];
}
return self;
}
- (void)drawRect:(CGRect)rect {
[_image drawAtPoint:CGPointMake(0, 0)];
}
- (void)dealloc {
[_image release];
[super dealloc];
}
@end
画像ファイルはUIImageクラスのimageNamedメソッドを利用します。イメージの描画はdrawAtPointを利用します。そして、deallocのタイミングでイメージのreleaseを忘れずに行います。
実行結果


0 件のコメント:
コメントを投稿