[objective-c]画像(UIImage)をカメラロールに保存する

UIImageをカメラロールに保存する方法はこうします。
下記コードのsavePhotoを実行したらすぐに保存が開始されますが、
保存が終わるまで時間が空くので、その間はローディングのぐるぐるみたいのを出しておくと良いでしょう。

-(void)savePhoto:(UIImage*)orizinalSizeImage{
    UIImageWriteToSavedPhotosAlbum(orizinalSizeImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
//写真保存後にコールバックされる
-(void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo{
    if(error){//エラーのとき
        
    }else{//保存できたとき
        
    }
}

[iPhone Apps]OldFilmPhoto(古映画フォト)

iPhoneアプリ「古映画フォト」をリリースしました。
古映画フォトは写真を昔のフィルム映画のような写真に加工するアプリです。
Twitter,Facebookに対応しており、作った写真をすぐにアップロードすることが出来ます。
彩度、ぼかしの度合いを調整して自分好みの設定で写真を加工しましょう。

主な機能
■撮った写真または保存されている写真を昔のフィルム映画のような写真に加工できます。
■彩度を調整できます。
■セピア具合を調整できます。
■ぼかしを調整できます。
■写真をTwitterへアップロードできます。
■写真をFacebookへアップロードできます。
■写真を使用中のiPhone,iPodへ保存できます。

OldFilmPhoto is photo retouch App.
Enable to convert the photo that look like old film.
You can upload photos to Twitter,Facebook.
Enable to adjust the saturation, sepia and blurring.

Features
■Enable to convert the photo that look like old film.
■Enable to adjust saturation.
■Enable to adjust sepia.
■Enable to adjust blurring.
■Enable to upload photo to Twitter.
■Enable to upload photo to Facebook.
■Enable to save photo to your device.


古映画フォト
古映画フォト


OldFilmPhoto

古映画フォト古映画フォト

古映画フォト古映画フォト

[objective-c]Camera

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

カメラ、アルバムから画像を取得する時こうします。
まずデリゲートメソッド使用のためのの設定
下記の例だと「CameraViewController.h」にこれを記述する。

@protocol UIImagePickerControllerDelegate;
@protocol UINavigationControllerDelegate;
@interface CameraViewController : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate>

そして、なんやかんやの処理を「CameraViewController.m」へ

//カメラ、アルバムから画像をもらった時
-(void)whenCameraDone:(UIImage*)omg{
    
}

//アラートでok押したとき
-(void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertno==1) {//NoCamera
        [self whenCameraDone:nil];
    }else if(alertno==2){//NoAlbum
        [self whenCameraDone:nil];
    }
}

//カメラが使えないときのアラート
-(void)alertNoCamera{
    alertno=1;
    UIAlertView *al=[[UIAlertView alloc]
                     initWithTitle:NSLocalizedString(@"NoCamera", nil)
                     message:nil
                     delegate:self
                     cancelButtonTitle:nil
                     otherButtonTitles:@"OK",nil];
    [al show];
    [al release];
}
-(void)takePhoto{
    UIImagePickerControllerSourceType pst=UIImagePickerControllerSourceTypeCamera;
    if([UIImagePickerController isSourceTypeAvailable:pst]){
        UIImagePickerController *pic=[[UIImagePickerController alloc]init];
        pic.delegate=self;
        pic.sourceType=pst;
        [self presentModalViewController:pic animated:YES];
        [pic release];
    }else{
        //カメラが使えないとき
        [self alertNoCamera];
    }
}

//撮影,画像選択>使用時
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *originalImage=[info objectForKey:UIImagePickerControllerOriginalImage];
    [self dismissModalViewControllerAnimated:YES];
    [self whenCameraDone:originalImage];
}
//撮影キャンセル時
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    //[self dismissViewControllerAnimated:YES];
    [self dismissModalViewControllerAnimated:YES];
    [self whenCameraDone:nil];
}

//Albumが使えないときのアラート
-(void)alertNoAlbum{
    alertno=2;
    UIAlertView *al=[[UIAlertView alloc]
                     initWithTitle:NSLocalizedString(@"NoAlbum", nil)
                     message:nil
                     delegate:self
                     cancelButtonTitle:nil
                     otherButtonTitles:@"OK",nil];
    [al show];
    [al release];
}
-(void)openAlbum{
    UIImagePickerControllerSourceType pst=UIImagePickerControllerSourceTypePhotoLibrary;
    if([UIImagePickerController isSourceTypeAvailable:pst]){
        UIImagePickerController *pic=[[UIImagePickerController alloc]init];
        pic.delegate=self;
        pic.sourceType=pst;
        [self presentModalViewController:pic animated:YES];
        [pic release];
    }else{
        //Albumが使えないとき
        [self alertNoAlbum];
    }
}

で使うときは下記のようにメソッドを実行する。

//カメラ
[self takePhoto];

//アルバム
[self openAlbum];

結構大変ですね。

[cocos2d]CCLabelTTF

cocos2dで文字列を表示させたい時はCCLabelTTFを使います。

使い方

CCLabelTTF*sizelabel=[CCLabelTTF labelWithString:@"ImageSize" fontName:@"HiraKakuProN-W6" fontSize:12];
[sizelabel setAnchorPoint:ccp(0.0,1.0)];
sizelabel.positionInPixels=ccp(100.0,200.0);
[self addChild:sizelabel z:19];

[sizelabel setString:@"hoge"];

[objective-c]画像(UIImage)をリサイズする

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

- (UIImage*)resizedImage:(UIImage *)img size:(CGSize)size{
    CGFloat widthRatio  = size.width  / img.size.width;
    CGFloat heightRatio = size.height / img.size.height;
    if (widthRatio==1.0 && heightRatio==1.0) {
        return img;
    }
    CGSize resizedSize = CGSizeMake(img.size.width*widthRatio, img.size.height*heightRatio);
    
    UIGraphicsBeginImageContext(resizedSize);
    
    [img drawInRect:CGRectMake(0, 0, (int)resizedSize.width, (int)resizedSize.height)];
    UIImage* resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return resizedImage;
}
//元の画像
UIImage*img=[UIImage imageNamed:@"hoge.png"];
CGRect*rect=CGRectMake(0.0, 0.0, 10.0, 20.0);

//リサイズした画像
UIImage*resizedImage=[self resizedImage:img rect:rect];

[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:@"."];