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

原地址: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 ); 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值