iPhone应用程序 将图片保存到相册实例

1:原文摘自:http://mobile.51cto.com/iphone-278615.htm

iPhone应用程序 将图片保存到相册实例是本文要介绍的内容,主要是以代码来实现本文要表现的内容,进入话题。有时候你的应用需要将应用中的图片保存到用户iPhone或者iTouch的相册中。 可以使用UIKit的这个类方法来完成。

  1. void UIImageWriteToSavedPhotosAlbum (    
  2.    UIImage  *image,    
  3.    id       completionTarget,    
  4.    SEL      completionSelector,    
  5.    void     *contextInfo    
  6. );    
  7. void UIImageWriteToSavedPhotosAlbum (  
  8.    UIImage  *image,  
  9.    id       completionTarget,  
  10.    SEL      completionSelector,  
  11.    void     *contextInfo  
  12. );  

image

要保存到用户设备中的图片

completionTarget

当保存完成后,回调方法所在的对象

completionSelector

当保存完成后,所调用的回调方法。 形式如下:

  1. - ( void ) image: ( UIImage *) image  
  2.     didFinishSavingWithError: ( NSError *) error  
  3.     contextInfo: ( void *) contextInfo; 

contextInfo

可选的参数,保存了一个指向context数据的指针,它将传递给回调方法。

比如你可以这样来写一个存贮照片的方法:

  1. // 要保存的图片   
  2.   UIImage *img = [ UIImage imageNamed:@"ImageName.png" ] ;     
  3.    
  4.   // 保存图片到相册中   
  5.   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.   }    
  15. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error  
  16.              contextInfo:(void *)contextInfo  
  17.   {  
  18.     // Was there an error?  
  19.     if (error != NULL)  
  20.     {  
  21.       // Show error message…  
  22.    
  23.     }  
  24.     else  // No errors  
  25.     {  
  26.       // Show message image successfully saved  
  27.     }  
  28.   }  

保存当前视图:

  1. #import  <QuartzCore/QuartzCore.h>   
  2.  
  3. UIGraphicsBeginImageContext(currentView.bounds .size ); //currentView 当前的 view   
  4.  
  5. [currentView. layer  renderInContext: UIGraphicsGetCurrentContext()];   
  6.  
  7. UIImage *viewImage =  UIGraphicsGetImageFromCurrentImageContext();   
  8.  
  9. UIGraphicsEndImageContext();  
  10.  
  11. UIImageWriteToSavedPhotosAlbum(viewImage, nil , nil , nil ); 

小结:iPhone应用程序 将图片保存到相册实例的内容介绍完了,希望本文对你有所帮助!

posted on 2011-12-06 21:49  wtq  阅读(593)  评论(1编辑  收藏  举报