前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS·长按保存图片到相册:系统原生UIActionSheet与UIAlertView,UIAlertController等方案

iOS·长按保存图片到相册:系统原生UIActionSheet与UIAlertView,UIAlertController等方案

作者头像
陈满iOS
发布2018-09-10 11:17:18
1.8K0
发布2018-09-10 11:17:18
举报
文章被收录于专栏:陈满iOS

场景: 在一个VC中,为一个UICollectionViewCell中的图片添加长按图片保存的事件。

长按保存图片

  • 前提:infoPlist中添加相应权限:Privacy - Photo Library Additions Usage Description。否则进行保存图片的时候APP会奔溃。

image.png

  • VC背景,及需要遵守的代理
代码语言:javascript
复制
@interface CustomerImageViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIActionSheetDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
@property (weak, nonatomic) IBOutlet UILabel *pageLabel;

@property (nonatomic, strong) NSMutableArray<NSString *> *imageUrlStrArr;
@property (nonatomic, strong) UIImage *tempImage;

1. UIActionSheet实现底部弹框

  • 给CollectionViewCell中的UIImageView添加事件
代码语言:javascript
复制
#pragma - mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imageUrlStrArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([MyCollectionViewCell class]) forIndexPath:indexPath];
    //传数据
    [cell setImageUrlStr:self.imageUrlStrArr[indexPath.item]];
    //事件
    UILongPressGestureRecognizer *ges = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
    [cell.imageView addGestureRecognizer:ges];
    return cell;
}

-(void)longPressAction:(UILongPressGestureRecognizer*)gesture {
    if(gesture.state == UIGestureRecognizerStateBegan)
    {
        UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消"destructiveButtonTitle:nil otherButtonTitles:@"保存图片",nil];

        actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
        [actionSheet showInView:self.view];
        
        UIImageView *imgView = (UIImageView*)[gesture view];
        _tempImage = imgView.image;
    }
}
  • 实现UIActionSheetDelegate代理方法
代码语言:javascript
复制
#pragma - mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet*)actionSheet didDismissWithButtonIndex:  (NSInteger)buttonIndex
{
    if(buttonIndex ==0) {
        if(_tempImage){
            UIImageWriteToSavedPhotosAlbum(_tempImage,self,@selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:),nil);
        }else{
            [Toast showCenterWithText:@"保存失败:没有网络"];
        }
    }
}

- (void)imageSavedToPhotosAlbum:(UIImage*)image didFinishSavingWithError: (NSError*)error contextInfo:(void*)contextInfo
{
    NSString*message =@"";
    
    if(!error) {
        
        message =@"成功保存到相册";
        
        UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"message:message delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil];
        
        [alert show];
        
    }else{
        message = [error description];
        
        UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"message:message delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil];
        
        [alert show];
        
    }
}

长按保存事件

保存成功提示

2. UIAlertView实现中部弹框

  • 修改点1:VC遵守的协议
代码语言:javascript
复制
@interface CustomerImageViewController ()<UICollectionViewDataSource,UICollectionViewDelegate, UIAlertViewDelegate >
  • 修改点2:VC添加两个属性
代码语言:javascript
复制
@property (nonatomic , strong) UIAlertView *myAlertView;
@property (nonatomic , strong) UIAlertView *myAlertView2;
  • 修改点3:longPressAction方法的实现
代码语言:javascript
复制
-(void)longPressAction:(UILongPressGestureRecognizer*)gesture {
    if(gesture.state == UIGestureRecognizerStateBegan)
    {
        UIImageView *imgView = (UIImageView*)[gesture view];
        _tempImage = imgView.image;
        
        self.myAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您要保存当前图片到相册中吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"保存",nil];
        [self.myAlertView show];
    }
}
  • 修改点4:实现UIAlertViewDelegate代理方法
代码语言:javascript
复制
#pragma - mark -  UIAlertViewDelegate 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        // 保存照片(获取到点击的 image)
        //        NSInteger i = self.scroll.contentOffset.x / self.scroll.bounds.size.width;
        if (_tempImage) {
            UIImageWriteToSavedPhotosAlbum(_tempImage, self, @selector(image:didFinshSavingWithError:contextInfo:), NULL);
        }
    }
}

// 保存图片错误提示方法
- (void)image:(UIImage *)image didFinshSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSString *mes = nil;
    if (error != nil) {
        mes = @"保存图片失败";
    } else {
        mes = @"保存图片成功";
    }
    self.myAlertView2 = [[UIAlertView alloc] initWithTitle:@"提示" message:mes delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
    [self.myAlertView2 show];
    [NSTimer scheduledTimerWithTimeInterval:0.8f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
}

- (void)performDismiss:(NSTimer *)timer {
    [self.myAlertView2 dismissWithClickedButtonIndex:0 animated:YES];
}

长按保存图片

3. UIAlertController实现底部/中部弹框

下面的修改点是针对上面的第二节2. UIAlertView的代码

  • 修改点1:longPressAction方法的实现
代码语言:javascript
复制
-(void)longPressAction:(UILongPressGestureRecognizer*)gesture {
    if(gesture.state == UIGestureRecognizerStateBegan)
    {
        UIImageView *imgView = (UIImageView*)[gesture view];
        _tempImage = imgView.image;
        
        [self handleActionSheet];
    }
}
  • 修改点2:实现如上的handleActionSheet(添加UIAlertController)
代码语言:javascript
复制
- (void)handleActionSheet{
    
    UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *saveImageAction = [UIAlertAction actionWithTitle:@"保存图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (_tempImage) {
            UIImageWriteToSavedPhotosAlbum(_tempImage, self, @selector(image:didFinshSavingWithError:contextInfo:), NULL);
        }
    }];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [actionSheetController addAction:cancelAction];
    [actionSheetController addAction:saveImageAction];
    
    [self presentViewController:actionSheetController animated:YES completion:nil];
}

其中,把preferredStyle的参数UIAlertControllerStyleActionSheet换成UIAlertControllerStyleAlert,就是中部弹框了。

  • 相同点1:成功及错误处理
代码语言:javascript
复制
// 保存图片错误提示方法
- (void)image:(UIImage *)image didFinshSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSString *mes = nil;
    if (error != nil) {
        mes = @"保存图片失败";
    } else {
        mes = @"保存图片成功";
    }
    self.myAlertView2 = [[UIAlertView alloc] initWithTitle:@"提示" message:mes delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
    [self.myAlertView2 show];
    [NSTimer scheduledTimerWithTimeInterval:0.8f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
}

长按保存图片

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.03.30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. UIActionSheet实现底部弹框
  • 2. UIAlertView实现中部弹框
  • 3. UIAlertController实现底部/中部弹框
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档