iOS长按保存图片

来源:互联网 发布:知其雄兮守其雌 编辑:程序博客网 时间:2024/04/28 04:37

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

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

// 长按(longPress)

            UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longPressAction:)];

            [imageViewaddGestureRecognizer:longPress];

            

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

            longPress.minimumPressDuration =1;

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

            longPress.allowableMovement =100;

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

// 长按的手势

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress

{

    // 手势的状态

    if (longPress.state ==UIGestureRecognizerStateBegan) {

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

        

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

        [self.myAlertViewshow];

        [_myAlertViewrelease];

    }

    

}


- (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.viewviewWithTag: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 = [[UIAlertViewalloc] initWithTitle:@"提示"message:mes delegate:selfcancelButtonTitle:nilotherButtonTitles:nil];

    [self.myAlertView2show];

    [_myAlertView2release];

    [NSTimerscheduledTimerWithTimeInterval:0.8ftarget:selfselector:@selector(performDismiss:)userInfo:nilrepeats:NO];

}


- (void)performDismiss:(NSTimer *)timer

{

    [self.myAlertView2dismissWithClickedButtonIndex:0animated:YES];

}



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



0 0
原创粉丝点击