将图片保存在iPhone的相册中

本文介绍如何使用UIKit中的方法将应用内的图片保存到iOS设备的相册,并提供了具体的实现代码示例,包括保存图片的方法及回调处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址:http://blog.youkuaiyun.com/microchenhong/article/details/6524960

有时候你的应用需要将应用中的图片保存到用户iPhone或者iTouch的相册中。 可以使用UIKit的这个类方法来完成。

[cpp]  view plain copy
  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 ) ; 
 

回调方法看起来可能是这样:

[cpp]  view plain copy
  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 );

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值