[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{//保存できたとき
        
    }
}

[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];

結構大変ですね。

[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];

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

[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

[objective-c]NSUserDefaults

NSUserDefaultsは一時的に情報を記録出来るクラスです。
アプリをアップデートしたりすると消えます。多分。FlashのSharedObjectのようなものです。

使い方

-(void)saveData{
    NSUserDefaults *me=[NSUserDefaults standardUserDefaults];
    [me setFloat:prop1 forKey:@"prop1"];
    [me setFloat:prop2 forKey:@"prop2"];
    [me setFloat:prop3 forKey:@"prop3"];
    [me setFloat:prop4 forKey:@"prop4"];
    [me setFloat:prop5 forKey:@"prop5"];
    [me synchronize];
}
-(void)loadData{
    NSUserDefaults *me=[NSUserDefaults standardUserDefaults];
    NSArray*obj=[NSArray arrayWithObjects:@"0.05",@"-0.5",@"0.4",@"0.1",@"1.0", nil];//各変数の初期値
    NSArray*key=[NSArray arrayWithObjects:@"prop1",@"prop2",@"prop3",@"prop4",@"prop5", nil];
    NSDictionary *appDefaults = [NSDictionary dictionaryWithObjects:obj forKeys:key];
    [me registerDefaults:appDefaults];
    
    prop1=[me floatForKey:@"prop1"];
    prop2=[me floatForKey:@"prop2"];
    prop3=[me floatForKey:@"prop3"];
    prop4=[me floatForKey:@"prop4"];
    prop5=[me floatForKey:@"prop5"];
}
-(void)removeData{
    NSUserDefaults *me=[NSUserDefaults standardUserDefaults];
    [me removeObjectForKey:@"prop1"];
    [me removeObjectForKey:@"prop2"];
    [me removeObjectForKey:@"prop3"];
    [me removeObjectForKey:@"prop4"];
    [me removeObjectForKey:@"prop5"];
    [me synchronize];
}

initメソッドあたりでloadDataする。
saveDataで保存して、removeDataで消す。簡単。