引入

如何将图片保存到IOS自带的Photo Album中?

实现

在一个视图中有一个UIImageView,当单击此UIImageView,将UIImageView中的UIImage所代表的图片保存到Photo Album中。

代码

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if([touch tapCount] == 1)
{
NSLog(@"single click %@",[imageView image]);
UIImageWriteToSavedPhotosAlbum([imageView image], nil, nil,nil);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"存储照片成功"
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定义的方法上。一般是当完成后调用方法时使用,或者在完成时出错的处理。