[objective-c]画像(UIImage)を切り取る

iPhone Apps「魚眼フォト」などで使っている方法です。
いいと思います。

-(UIImage*)cropImage:(UIImage *)img rect:(CGRect)rect{
    CGImageRef imgref=CGImageCreateWithImageInRect([img CGImage],rect);
    UIImage *result=[UIImage imageWithCGImage:imgref];
    CGImageRelease(imgref);
    return result;
}
//元の画像
UIImage*img=[UIImage imageNamed:@"hoge.png"];
CGRect*rect=CGRectMake(0.0, 0.0, 10.0, 20.0);

//切り取った画像
UIImage*cropimg=[self cropImage:img rect:rect];

[cocos2d]線を引く

cocos2dで線を引くのはこうやる。
メソッド名は「draw」じゃないとダメ。
[self draw];とか書かなくてよい。
メソッド名を「drawLine」にして[self drawLine];で実行してもダメ。

-(void)draw{
    glColor4f(1.0, 0.0, 0.0, 1.0);
    glLineWidth(1.0f);
    CGPoint p1,p2;
    p1=CGPointMake(0.0,0.0);
    p2=CGPointMake(100.0,100.0);
    ccDrawLine(p1, p2);
}

[cocos2d]CCSequence#02 CCCallFuncND

iPhone Game「社畜!ナカムラくん」の制作で実践していた方法です。
CCSequenceを使ってメソッドを実行するとき
前のエントリーではCCCallFuncを使っていましたが
メソッドに値を渡したいときはCCCallFuncNDを使うとよい。
使い方はこんな感じ。
下記の例だとdataに渡す値をretainしないとEXC_BAD_ACCESSになる。

id act_func0,act_func1,act_func2;
CCSequence* seq;
act_func0=[CCCallFuncND actionWithTarget:self selector:@selector(playSE:data:) data:[[NSNumber numberWithInt:15]retain]];
act_func1=[CCCallFuncND actionWithTarget:self selector:@selector(playSE:data:) data:[[NSNumber numberWithInt:13]retain]];
act_func2=[CCCallFuncND actionWithTarget:self selector:@selector(playSE:data:) data:[[NSNumber numberWithInt:14]retain]];
seq=[CCSequence actions:act_func0,[CCDelayTime actionWithDuration:0.6f],act_func1,[CCDelayTime actionWithDuration:0.9f],act_func2,nil];
[se runAction:seq];

-(void)playSE:(id)n data:(id)v{
    NSNumber* dt=(NSNumber*)v;
    int seno=[dt intValue];
    switch (seno) {
        case 0:
            [[SimpleAudioEngine sharedEngine] playEffect:@"girigiri.mp3"];
            break;
        default:
            break;
    }
    [v release];
    v=nil;
}

[iPhone Apps]VintagePhoto(古フォト)

iPhoneアプリ「古フォト」をリリースしました。
古フォト(ふるふぉと)は写真を昔撮ったような写真に加工するアプリです。
Twitter,Facebookに対応しており、作った写真をすぐにアップロードすることが出来ます。
色むら、セピアの度合いを調整して自分好みの設定で写真を加工しましょう。

主な機能
■撮った写真または保存されている写真を昔撮ったような写真に加工できます。
■白枠の太さを調整できます。
■彩度を調整できます。(白黒←→カラー)
■セピア具合を調整できます。
■薄れ具合を調整できます。
■色むらを調整できます。
■写真をTwitterへアップロードできます。
■写真をFacebookへアップロードできます。
■写真を使用中のiPhone,iPodへ保存できます。

VintagePhoto is photo retouch App.
Enable to retouch a photo to old photo.
Enable to upload photos to Twitter,Facebook.

Features
■Enable to retouch a photo to old photo.
■Enable to adjust frame.
■Enable to adjust saturation.
■Enable to adjust sepia.
■Enable to adjust brightness.
■Enable to adjust color unevenness.
■Enable to upload the photo to Twitter.
■Enable to upload the photo to Facebook.
■Enable to save photo to your device.


古フォト
古フォト


VintagePhoto

古フォト古フォト

古フォト古フォト

[objecitve-c]NSString#01

NSStringの使い方メモ#01

生成

NSString*s=@"やぁ";

文字とか数字とかと結合

NSString*t=@"あいうえお";
NSString*s=[NSString stringWithFormat:@"%@ABC%fDEF%d",t,3.14159265,52];
NSLog(@"%@",s);

出力結果
あいうえおABC3.141593DEF52

型変換

NSString*t=@"323";
int d=[t intValue];
float f=[t floatValue];
double w=[t doubleValue];
BOOL flg=[t boolValue];

文字数

NSString*t=@"323";
int d=[t length];
NSLog(@"length:%d",d);

出力結果
length:3

比較

NSString*t=@"ktyr";
BOOL flg=[t isEqualToString:@"ktyr"];
NSLog(@"flg:%d",(flg)?1:0);

文字列分割

NSString*t=@"ktyr.report";
NSArray*n=[t componentsSeparatedByString:@"."];

[cocos2d]CCMenu

cocos2dでボタンを作るときはCCMenuが便利です。
使い方はこう

CCNode*setting=[CCNode alloc]init];
[self addChild:setting];

CCMenu*settingmenu=[[CCMenu alloc]initWithItems:nil vaList:nil];
[setting addChild:settingmenu z:21];
settingmenu.positionInPixels=CGPointZero;

CCMenuItemImage*item=[CCMenuItemImage itemFromNormalImage:@"ボタンの画像" selectedImage:@"タッチした時のボタンの画像" target:self selector:@selector(whenSettingTap:)];
[item setAnchorPoint:CGPointMake(0.0, 1.0)];
item.positionInPixels=ccp(195.0, 480.0-0.0);
[settingmenu addChild:item z:22 tag:5];

上記の例ではCCMenuを作ってからCCMenuItemを追加していますが、

CCNode*setting=[CCNode alloc]init];
[self addChild:setting];

CCMenuItemImage*item=[CCMenuItemImage itemFromNormalImage:@"ボタンの画像" selectedImage:@"タッチした時のボタンの画像" target:self selector:@selector(whenSettingTap:)];
[item setAnchorPoint:CGPointMake(0.0, 1.0)];
item.positionInPixels=ccp(195.0, 480.0-0.0);
item.tag=5;

CCMenu*settingmenu=[CCMenu menuWithItems:item, nil];
[setting addChild:settingmenu z:21];
settingmenu.positionInPixels=CGPointZero;
settingmenu reorderChild:item z:22];

というクラスメソッドを使った書き方も出来ます。
私は上のほうが好きです。

[cocos2d]サウンド

iPhone Game「社畜!ナカムラくん」で実践した方法。

initメソッドあたりでmp3ファイルをpreloadしておきます。

#import "SimpleAudioEngine.h"
[[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:@"bgm.mp3"];//BGM
[[SimpleAudioEngine sharedEngine] preloadEffect:@"clock.mp3"];//サウンドエフェクト

BGMを鳴らすときは

[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"bgm.mp3" loop:YES];

サウンドエフェクトを鳴らすときは

[[SimpleAudioEngine sharedEngine] playEffect:@"clock.mp3"];

ボリュームの調整

[SimpleAudioEngine sharedEngine].backgroundMusicVolume=1.0;
[SimpleAudioEngine sharedEngine].effectsVolume=0.5;

BGM一時停止

[[SimpleAudioEngine sharedEngine] pauseBackgroundMusic];

BGM再開

[[SimpleAudioEngine sharedEngine] resumeBackgroundMusic];

[objective-c]グローバルメソッド,定数

グローバルメソッドを設定する。
AppDelegate.hの@interfaceの手前に記述する。pchに記述でもok。
下記は2座標間の距離を返すメソッドと指定した範囲で乱数を返すメソッド。

#define get_dist(px1,py1,px2,py2) sqrt((px2-px1)*(px2-px1)+(py2-py1)*(py2-py1))
#define get_random(st,en) (arc4random()%ABS(en-st))+st;

定数はこんな感じ

#define HOGEHOGE 0

[cocos2d]CCMoveTo 座標を移動させる

CCSpriteなどの座標を移動させる方法です。

CCMoveTo,CCRotateToなどがあり、FlashのTweenクラスのようなものです。
CCSpriteを今の座標から110.0,110.0へイーズインアウトで移動させる時

CCSprite*sp=[CCSprite spriteWithFile:@"sc02.png"];
[self addChild:sp];
id moveto=[CCMoveTo actionWithDuration:0.4 position:ccp(110.0,110.0)];
id ease=[CCEaseInOut actionWithAction:moveto rate:2.0];
[sp runAction:ease];

止める時

[sp stopAllActions];

参考サイト
cocos2d for iPhone: Class List

[cocos2d]CCSequence#01

CCSpriteなどを「移動して5秒待って回転してメソッド実行」のような複合アクションさせる方法です。
よく忘れるのでメモ

-(void)testFunc{
    NSLog(@"hello");
}
CCSprite*sp=[CCSprite spriteWithFile:@"sc02.png"];
[self addChild:sp];
id moveto=[CCMoveTo actionWithDuration:0.4 position:ccp(110.0,110.0)];
id ease=[CCEaseInOut actionWithAction:moveto rate:2.0];
id roll=[CCRotateTo actionWithDuration:1.0 angle:74.0];
id act_func=[CCCallFunc actionWithTarget:self selector:@selector(testFunc)];
CCSequence*seq=[CCSequence actions:ease,[CCDelayTime actionWithDuration:5.0f],roll,act_func,nil];
[sp runAction:seq];

参考サイト
CCSequence Class Reference