將圖片保存在iPhone的相冊中

原文地址:http://blog.csdn.net/microchenhong/article/details/6524960

有時候你的應用需要將應用中的圖片保存到用戶iPhone或者iTouch的相冊中。 可以使用UIKit的這個類方法來完成。

  1. void UIImageWriteToSavedPhotosAlbum (  
  2.    UIImage  *image,  
  3.    id       completionTarget,  
  4.    SEL      completionSelector,  
  5.    void     *contextInfo  
  6. );  

 

image
要保存到用戶設備中的圖片

completionTarget
當保存完成後,回調方法所在的對象

completionSelector
當保存完成後,所調用的回調方法。 形式如下:

( void ) image: ( UIImage *) image
    didFinishSavingWithError: ( NSError *) error
    contextInfo: ( void *) contextInfo;
 

contextInfo
可選的參數,保存了一個指向context數據的指針,它將傳遞給回調方法。

比如你可以這樣來寫一個存貯照片的方法:

// 要保存的圖片 
  UIImage *img = [ UIImage imageNamed:@"ImageName.png" ] ;   
 
  // 保存圖片到相冊中 
  UIImageWriteToSavedPhotosAlbum( img, self, @selector (image:didFinishSavingWithError:contextInfo:) , nil ) ; 
 

回調方法看起來可能是這樣:

  1. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error  
  2.              contextInfo:(void *)contextInfo  
  3.   {  
  4.     // Was there an error?  
  5.     if (error != NULL)  
  6.     {  
  7.       // Show error message…  
  8.    
  9.     }  
  10.     else  // No errors  
  11.     {  
  12.       // Show message image successfully saved  
  13.     }  
  14.   }  


保存當前視圖:

#import  <QuartzCore/QuartzCore.h>

UIGraphicsBeginImageContext(currentView.bounds .size ); //currentView 當前的 view

[currentView. layer  renderInContext: UIGraphicsGetCurrentContext()];

UIImage *viewImage =  UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(viewImage, nil , nil , nil );

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章