UIImageをカメラロールに保存する。

参考サイト
Objective-Cと戦うブログ: UIImageをフォトライブラリに保存する
iPhone:画像をライブラリに保存するメソッドUIImageWriteToSavedPhotosAlbumの注意事項 | mthr Blog+
UIImageをカメラロールに保存(iOS, Objective-C) – nktmemo

コードとしては
UIImageWriteToSavedPhotosAlbum(イメージ, self, @selector(完了時に呼ばれるメソッド), nil);
って感じ。
カメラロールに保存される場合はファイル名を指定できないぽい。

//画像を保存する
-(void)saveImage
{
    //保存したい画像
    UIImage *_saveImage = [UIImage imageNamed:@"image.png"];

    UIImageWriteToSavedPhotosAlbum(_saveImage, self, @selector(savingImageIsFinished:didFinishSavingWithError:contextInfo:), nil);
}

// 完了を知らせる
- (void) savingImageIsFinished:(UIImage *)_image didFinishSavingWithError:(NSError *)_error contextInfo:(void *)_contextInfo
{
    if(_error){//エラーのとき
        // コントローラを生成
        UIAlertController * ac =
        [UIAlertController alertControllerWithTitle:@"Error"
                                            message:@"Save failed ."
                                     preferredStyle:UIAlertControllerStyleAlert];
        
        
        // OK用のアクションを生成
        UIAlertAction * okAction =
        [UIAlertAction actionWithTitle:@"OK"
                                 style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction * action) {
                                   // ボタンタップ時の処理
                                   NSLog(@"OK button tapped.");
                               }];
        
        // コントローラにアクションを追加
        [ac addAction:okAction];
        
        // アラート表示処理
        [self presentViewController:ac animated:YES completion:nil];
    }else{//保存できたとき
        return;
    }
}

コメントを残す

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