Ryusuke Fuda's Tech Blog

Softweare Enginier about Web, iOS, Android.

UIImage document保存, 取得

■ だいたいこんな感じでできる。

    UIImage *tookImg = [info objectForKey:UIImagePickerControllerOriginalImage];


    NSData *data = UIImageJPEGRepresentation(tookImg, 0.8f);
    
    // ここではデータを保存する為に一般的に使われるDocumentsディレクトリ
    NSString *filePath = [NSString stringWithFormat:@"%@/test.png" , [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]];
    
    // NSDataのwriteToFileメソッドを使ってファイルに書き込みます
    // atomically=YESの場合、同名のファイルがあったら、まずは別名で作成して、その後、ファイルの上書きを行います
    if ([data writeToFile:filePath atomically:YES]) {
        NSLog(@"save OK");
    } else {
        NSLog(@"save NG");
    }
    [self dismissViewControllerAnimated:YES completion:nil];
    
    // ファイルから画像データを読み込み、UIImageオブジェクトを作成
    UIImage *image= [[UIImage alloc] initWithContentsOfFile:filePath];
    _testImageView.image = image;


参考 )

http://www.tamrable.info/?p=754
http://ebisu.surbiton.jp/sysblog/2011/06/uiimagepicker.html
http://iaseteam.eshizuoka.jp/e1068667.html
http://www.yoheim.net/blog.php?q=20111031