UIImageをPNGで共有フォルダに保存する。

参考サイト
[XCODE] iPhoneで画像をアプリケーション内に保存するには – YoheiM .NET
UIImage document保存, 取得 – Ryusuke Fuda’s Tech Blog
UIImageの画像データをPNGファイルに保存する方法 – 強火で進め

UIImagePNGRepresentationと、
writeToFile: atomically: ってとこがキモかな。

-(void)sharedFileSave:(UIImage *)sender
{
    NSLog(@"しぇあどふぁいるせーぶ");
    //保存する画像をPNGでnsdataに変換?
    UIImage *image = sender;
    NSData *data = UIImagePNGRepresentation(image);
    
    //ドキュメントフォルダ(共有フォルダ)のパスの取得
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *DocumentsDirPath = [paths lastObject];//これがパスになるらしい。
    
    //保存する名前を決める。
    NSString *exportFilename = @"sample2.png";
    
    //保存先のパスと名前を合体させたパスを作成。
    NSString *exportFilePath = [DocumentsDirPath stringByAppendingPathComponent:exportFilename];
    
    ;
    if ([data writeToFile: exportFilePath atomically:YES]) {
        NSLog(@"ほぞんOK");
    } else {
        NSLog(@"ほぞんError");
    }
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です