在.h文件里写入:
voidUIImageWriteToSavedPhotosAlbum(
UIImage*image,
id completionTarget,
SEL completionSelector,
void *contextInfo
UIImage*image,
id completionTarget,
SEL completionSelector,
void *contextInfo
);
#pragma mark IOS的UIKit Framework提供了UIImageWriteToSavedPhotosAlbum方法对图像进行保存,该方法会将image保存至用户的相册中
//参数1. image:带保存的图片UIImage对象
//参数2. completionTarget:图像保存至相册后调用completionTarget指定的selector(可选)
//参数3. completionSelector:completionTarget的方法对应的选择器,相当于回调方法,需满足以下格式
/*
- (void) image: (UIImage *) image
didFinishSavingWithError: (NSError *) error
contextInfo: (void *) contextInfo;
*/
//参数2. completionTarget:图像保存至相册后调用completionTarget指定的selector(可选)
//参数3. completionSelector:completionTarget的方法对应的选择器,相当于回调方法,需满足以下格式
/*
- (void) image: (UIImage *) image
didFinishSavingWithError: (NSError *) error
contextInfo: (void *) contextInfo;
*/
// contextInfo指定了在回调中可选择传入的数据
在.m文件里写入:
一.在 viewDidLoad里写入
//要保存的图片
UIImage*saveImage = [UIImageimageNamed:@"6.JPG"];
//保存图片到相册中
[selfsaveImageToPhotos:saveImage];
- (void)saveImageToPhotos:(UIImage*)savedImage
{
UIImageWriteToSavedPhotosAlbum(savedImage,self,@selector(image:didFinishSavingWithError:contextInfo:),nil);
}
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error
contextInfo:(void*)contextInfo
{
NSString *msg = nil;
if (error != NULL) {
msg = @"保存图片失败";
}else{
msg = @"保存图片成功";
}
UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:@"保存图片结果提示"message:msgdelegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil];
[alert show];
{
UIImageWriteToSavedPhotosAlbum(savedImage,self,@selector(image:didFinishSavingWithError:contextInfo:),nil);
}
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error
contextInfo:(void*)contextInfo
{
NSString *msg = nil;
if (error != NULL) {
msg = @"保存图片失败";
}else{
msg = @"保存图片成功";
}
UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:@"保存图片结果提示"message:msgdelegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil];
[alert show];
}