保存到相册

@interface ViewController ()
void UIImageWriteToSavedPhotosAlbum (
                               
                                     UIImage  * image,
                                  
                                     id      completionTarget,
                                  
                                     SEL      completionSelector,
                                  
                                     void    *contextInfo
                                 
                                     );
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
   
    UIImage *imagName = [UIImage imageNamed:@"submit"];
   
    [self saveImageToPhotos:imagName];
}


-(void)saveImageToPhotos:(UIImage*)savedImage

{
  
    UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
 
}

// 指定回调方法

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

{
 
    NSString *msg = nil ;
 
    if(error != NULL){
   
        msg = @"保存图片失败" ;
      
    }else{
     
        msg = @"保存图片成功" ;
     
    }
   
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存图片结果提示"
                      
                                                    message:msg
                      
                                                   delegate:self
                         
                                          cancelButtonTitle:@"确定"
                     
                                          otherButtonTitles:nil];
  
    [alert show];
   
}
 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

     if([touch tapCount] == 1)

     {

       NSLog(@"single click %@",[imageView image]);

       UIImageWriteToSavedPhotosAlbum([imageView image], nilnil,nil);

       UIAlertView *alert = [[UIAlertView allocinitWithTitle:@"存储照片成功"

                                                 message:@"您已将照片存储于图片库中,打开照片程序即可查看。"

                                                 delegate:self

                                         cancelButtonTitle:@"OK"

                                         otherButtonTitles:nil];

       [alert show];

       [alert release];

     }

}

说明

UIImageWriteToSavedPhotosAlbum是UIKit框架中的一个函数。

这里说一下后面三个参数的含义:

void UIImageWriteToSavedPhotosAlbum (

   UIImage  *image,

   id       completionTarget,

   SEL      completionSelector,

   void     *contextInfo

);

 

id是target对象,sel是selector,即target对象上的方法名,contextInfo是任意指针,会传递到selector定义的方法上。一般是当完成后调用方法时使用,或者在完成时出错的处理。

posted @ 2016-04-17 23:57  huangyan1022  阅读(120)  评论(0编辑  收藏  举报