IOS之长按图片保存到本地

我们在开发的过程中,很多项目中都有很多各式各样的图片,这样一来,难免就会有保存图片到相册的功能,其实很简单,今天空下来了,我详细的写一下:

首先先创建一个UIImageView对象:

@property(nonatomic,strong)UIImageView *img;

然后懒加载一下:

-(UIImageView *)img{

    if(!_img) {

        _img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.jpg"]];

     }

    return _img;

}

在viewDidLoad中创建一个长按的手势添加到图片对象上(一般都是长按图片弹出二次确认框保存图片到相册):

//图片添加到主视图上

[self.view addSubview:self.img];

    //1.创建长按手势

    UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(imglongTapClick:)];

    //2.开启人机交互

    self.img.userInteractionEnabled = YES;

    //3.添加手势

    [self.img addGestureRecognizer:longTap];

然后就是实现长按手势的方法

#pragma mark 长按手势弹出警告视图确认

-(void)imglongTapClick:(UILongPressGestureRecognizer*)gesture{

    if(gesture.state==UIGestureRecognizerStateBegan){

        UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认保存图片到相册吗" preferredStyle:UIAlertControllerStyleActionSheet];

        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

            NSLog(@"取消保存图片");

        }];

      UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

           NSLog(@"确认保存图片");

          // 保存图片到相册

  UIImageWriteToSavedPhotosAlbum(self.img.image,self,@selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:),nil);

          }];

        [alertControl addAction:cancel];

        [alertControl addAction:confirm];

        [self presentViewController:alertControl animated:YES completion:nil];

    }

}

然后执行保存图片后的回调方法

#pragma mark 保存图片后的回调

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

    NSString*message =@"提示";

    if(!error) {

        message =@"成功保存到相册";

        UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnullaction) {  }];

            [alertControl addAction:action];

            [self presentViewController:alertControl animated:YES completion:nil];

    }else{

            message = [errordescription];

           UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];

           UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnullaction) {  }];

          [alertControl addAction:action];

          [self presentViewController:alertControl animated:YES completion:nil];

   }

}


这样还没有完成,运行保存图片的时候会出错,因为还没有开启访问相册的权限,这个权限和在相册选取图片的权限可是不一样的,在info.plist中加入key为Privacy - Photo Library Additions Usage Description,Value随便写(例:需要访问您的本地相册)。好了,大功告成!!

你可能感兴趣的:(IOS之长按图片保存到本地)