将图片保存到相册(本地)

时间:2021-11-17 05:16:13

方法比较简单:

  //保存到相册
UIImageWriteToSavedPhotosAlbum(_fullImageView.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);

//保存后调用的方法:

- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
NSString *message = @"呵呵哒";
if (!error) {
message = @"成功保存到相册";
}else
{
message = [error description];
}
NSLog(@"message is %@",message);
}

以上两段代码就可以保存到图片到本地。一般都是长按保存图片,添加长按手势:

//添加长按手势到_scrollview
UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(zAction)];
//这里我是添加到滑动视图上了,滑动视图上有一个imageView 
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">//设置长按最短时间</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(112, 61, 170);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"></span><pre name="code" class="objc"><span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;"></span><pre name="code" class="objc">press.minimumPressDuration = 1;

 
 
 

 
 
[_scrollview addGestureRecognizer:press];

长按后调用的方法

//长按图片调用的方法。
- (void)zAction
{

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"是否保存图片到本地" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

[alertView show];


}

//提示框调用的代理方法:
//提示框的代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
//保存到相册
UIImageWriteToSavedPhotosAlbum(_fullImageView.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);

//获得沙盒路径 ------ 保存到沙盒
// NSString *path = NSHomeDirectory();
//
// //设置图片保存的路径
// NSString *imagePath = [path stringByAppendingString:@"/Documents"];
// //将图片写入本地
// [UIImagePNGRepresentation(_fullImageView.image) writeToFile:imagePath atomically:YES];
//

}
}

//保存到相册成功后调用的方法- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{    NSString *message = @"呵呵哒";    if (!error) {        message = @"成功保存到相册";    }else    {        message = [error description];    }    NSLog(@"message is %@",message);}