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

移动开发 iOS
本文介绍的是iPhone应用程序 将图片保存到相册实例,主要介绍了对图片的操作。先来看内容。

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应用程序 将图片保存到相册实例的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-07-26 16:43:59

iPhone Web 服务器

2011-07-26 11:13:15

iPhone PXL

2011-07-27 17:45:29

iPhone 模拟器 图片

2011-07-19 10:56:15

iPhone 控制器 视图

2011-07-19 10:42:41

iPhone 应用程序 模型

2011-07-19 11:12:07

iPhone 控制器

2011-07-21 10:47:37

iPhone Cocoa 委托

2011-07-26 09:41:23

iPhone xcode Mac OS X

2010-08-27 10:41:41

iPhone核心应用程序

2011-07-20 15:58:58

iPhone 应用程序 生命周期

2011-07-19 14:36:32

iPhone

2011-08-05 13:49:53

iPhone 应用 开发

2011-08-12 14:54:45

iPhone委托

2011-07-27 17:30:40

iPhone Locate 定位

2011-07-21 15:56:32

iPhone 截屏

2012-05-07 09:53:10

jQuery

2009-12-04 10:36:29

iPhone应用程序

2012-05-24 15:49:35

HTML5

2011-07-26 16:33:56

iPhone Delegate

2011-07-28 13:59:40

iPhone App
点赞
收藏

51CTO技术栈公众号