2015-04-30 74 views
0

我用下面的代碼從互聯網上獲得的圖像如何在UITableViewCell中保存圖片到相冊與長按

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

} 
} 

那麼如何獲得我長時間拍攝的圖像?我想將它保存到相冊中,謝謝。

回答

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這裏發生了什麼,當手勢發生在處理程序中時,您可以訪問調用該手勢的UIImageView,並可以從中獲取圖像。您也可以將手勢附加到整個個人單元格(不確定您的用例是什麼),並根據您的使用情況類似地提取ImageView和圖像。

+0

添加函數'longpresstodo'不起作用。 – spotcool

+0

ohh還有一件事,你需要設置爲UIImageView啓用交互,默認情況下它關閉。讓我修復答案 – Qazi

+0

我編輯了答案,它是否適合你(我測試了它在我身邊,但只是好奇,如果我留下了一些東西) – Qazi

相關問題