原文地址:http://blog.csdn.net/microchenhong/article/details/6524960
有時候你的應用需要將應用中的圖片保存到用戶iPhone或者iTouch的相冊中。 可以使用UIKit的這個類方法來完成。
- void UIImageWriteToSavedPhotosAlbum (
- UIImage *image,
- id completionTarget,
- SEL completionSelector,
- void *contextInfo
- );
image
要保存到用戶設備中的圖片
completionTarget
當保存完成後,回調方法所在的對象
completionSelector
當保存完成後,所調用的回調方法。 形式如下:
didFinishSavingWithError: ( NSError *) error
contextInfo: ( void *) contextInfo;
contextInfo
可選的參數,保存了一個指向context數據的指針,它將傳遞給回調方法。
比如你可以這樣來寫一個存貯照片的方法:
UIImage *img = [ UIImage imageNamed:@"ImageName.png" ] ;
// 保存圖片到相冊中
UIImageWriteToSavedPhotosAlbum( img, self, @selector (image:didFinishSavingWithError:contextInfo:) , nil ) ;
回調方法看起來可能是這樣:
- - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
- contextInfo:(void *)contextInfo
- {
- // Was there an error?
- if (error != NULL)
- {
- // Show error message…
- }
- else // No errors
- {
- // Show message image successfully saved
- }
- }
保存當前視圖:
#import <QuartzCore/QuartzCore.h>
UIGraphicsBeginImageContext(currentView.bounds .size ); //currentView 當前的 view
[currentView. layer renderInContext: UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil , nil , nil );