MWPhotoBrowser中添加长按保存图片功能

//方法调用写在viewDidLoad中
[self addLongPressToSaveImage];
//添加手势
- (void)addLongPressToSaveImage
{
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToSaveImage:)];
    longPressGesture.delegate = self;
    [self.view addGestureRecognizer:longPressGesture];
}

//长按方法
- (void)longPressToSaveImage:(UILongPressGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateBegan) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"是否保存到本地相册" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
    }
}

//alert代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1) {
        id  photo = _photos[_currentPageIndex];
        UIImage *img = [self imageForPhoto:photo];
        [self loadImageFinished:img];
    }
}

//保存图片到相册
- (void)loadImageFinished:(UIImage *)image
{
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}

//监听保存状态
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"保存失败" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }else{
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"保存成功" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }
    
}

你可能感兴趣的:(MWPhotoBrowser中添加长按保存图片功能)