iOS长按保存图片

在有的App中的图片你长按它就会提示你是否保存当前图片,这个效果看起来是不是挺高大上的啊,今天我就来给大家分享这个效果,下面写代码

下面的代码中imageView得首先创建好,就是你想要保存的那种图片,都在viewDidLoad中写

// 长按(longPress)

            UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];

            [imageView addGestureRecognizer:longPress];

            

            // 判定为长按手势 需要的时间

            longPress.minimumPressDuration = 1;

            // 判断期间,允许用户移动的距离

            longPress.allowableMovement = 100;

下面的代码是长按手势要实现的方法:也就是保存图片到本地相册,会弹出一个提示框点击保存的时候才会保存!

// 长按的手势

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress

{

    // 手势的状态

    if (longPress.state == UIGestureRecognizerStateBegan) {

        // 当手势的状态处于刚开始的状态

        

        self.myAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您要保存当前图片到相册中吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"保存", nil];

        [self.myAlertView show];

        [_myAlertView release];

    }

    

}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    if (buttonIndex == 1) {

        // 保存照片

        NSInteger i = self.scroll.contentOffset.x / self.scroll.bounds.size.width;

        UIImageView *myImageView = (UIImageView *)[self.view viewWithTag:10000 + i];

        UIImageWriteToSavedPhotosAlbum(myImageView.image, self, @selector(image:didFinshSavingWithError:contextInfo:), nil);

    }

}


// 保存图片错误提示方法

- (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];

    [_myAlertView2 release];

    [NSTimer scheduledTimerWithTimeInterval:0.8f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];

}


- (void)performDismiss:(NSTimer *)timer

{

    [self.myAlertView2 dismissWithClickedButtonIndex:0 animated:YES];

}



今天的代码就给大家分享到这,谢谢大家!



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值