在有的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];
}
今天的代码就给大家分享到这,谢谢大家!