如何通过长按将uitableviewcell中的图像保存到相册中

时间:2015-04-30 16:24:18

标签: ios uitableview uiimageview

我使用以下代码从互联网上获取图片

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
   NSURL *imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",image_url, [dic objectForKey:@"imageurl"]]];
__block UIActivityIndicatorView *activityIndicator;
__weak UIImageView *weakImageView = cell.userinformationimageView;
cell.userinformationimageView.backgroundColor = [UIColor clearColor];

[cell.userinformationimageView sd_setImageWithURL:imageUrl placeholderImage:nil options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
    if (!activityIndicator) {
        [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]];
        activityIndicator.center = weakImageView.center;
        [activityIndicator startAnimating];
    }
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    [activityIndicator removeFromSuperview];
    activityIndicator = nil;
}];
//more codes
}

有太多的细胞,每个细胞都有一个图像,我加上长按

UILongPressGestureRecognizer * longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo:)];
longPressGr.minimumPressDuration = 1.0;
[self.tableview addGestureRecognizer:longPressGr];

- (void)longPressToDo:(UILongPressGestureRecognizer *)gesture
{
if(gesture.state == UIGestureRecognizerStateBegan)
{
    CGPoint point = [gesture locationInView:self.tableview];
    NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:point];
    if(indexPath == nil) return;
    NSLog(@"---->%d",[indexPath row]);
    NSDictionary *dic = [self.tableData objectAtIndex:indexPath.row];

}
}

那么如何才能获得我长时间拍摄的图像?我想将它保存到专辑中,谢谢。

1 个答案:

答案 0 :(得分:0)

对于您要做的事情,有一个更简单的清洁解决方案。我可能会这样做

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSURL *imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",image_url, [dic objectForKey:@"imageurl"]]];
    __block UIActivityIndicatorView *activityIndicator;
    __weak UIImageView *weakImageView = cell.userinformationimageView;

    cell.userinformationimageView.backgroundColor = [UIColor clearColor];

    //Instead of whole TableView add the gesture to the Image itself
    UILongPressGestureRecognizer * longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo:)];
    longPressGr.minimumPressDuration = 1.0;
    [cell.userinformationimageView addGestureRecognizer:longPressGr];
     cell.userinformationimageView.userInteractionEnabled = YES;
    [cell.userinformationimageView sd_setImageWithURL:imageUrl placeholderImage:nil options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        if (!activityIndicator) {
            [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]];
            activityIndicator.center = weakImageView.center;
            [activityIndicator startAnimating];
        }
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
       [activityIndicator removeFromSuperview];
       activityIndicator = nil;
    }];
//more codes
}

在手势处理程序功能中执行此操作

- (void)longPressToDo:(UILongPressGestureRecognizer *)gesture
{
      //Just to be sure and avoid crashes
      if ([recognizer.view isKindOfClass:[UIImageView class]] ) {
         //Get the ImageView 
         UIImageView *temp = (UIImageView*)recognizer.view;
         UIImage *requiredImage = temp.image;
         //Do whatever you want with the image
    }     
}

所以基本上这里发生的是代替整个tableview,你将手势附加到你的Image,当手势发生在你的处理程序中时,你可以访问调用手势的UIImageView并从中获取图像。您还可以将手势附加到整个单元格(不确定您的用例是什么),同样提取ImageView和图像供您使用。