2011年11月25日金曜日

画像をPhotoLibralyに保存する


さて、前回作った画面キャプチャのUIImageって何に使うんだ?と思った人は多いでしょうね。
まあ、使い道としてはカメラやフォトライブラリから取得した写真をごにょごにょして、それをまた保存するなりメールで送りたいって感じでしょうかね。

コールバック関数の部分は別に nil にしてもらっても良いと思います。

-(void)saveToPhotoLibrary{

 UIImage* img = [self screenCapture];//前回の記事を呼んでちょ 
 NSData *pngData = UIImagePNGRepresentation(img);
 UIImage *screenImage = [[UIImage imageWithData:pngData] retain];
 
//コールバック関数の設定  
    SEL sel = @selector(savingImageIsFinished:didFinishSavingWithError:contextInfo:);
    UIImageWriteToSavedPhotosAlbum(screenImage, self, sel, nil);//ここのselが保存後に呼ばれる。

}



//コールバック関数

- (void)savingImageIsFinished:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void *)contextInfo{
   
    NSString* message;
    if(!error){
        message = @"success save";
    }else{
        message = @"fail save";
    }
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"保存" message:message
                              delegate:self
                              cancelButtonTitle:nil
                              otherButtonTitles:@"OK",nil];
    [alertView show];
    [alertView release];
}


0 件のコメント:

コメントを投稿