[英]how to save an image in uitableviewcell to album with long tap
我使用以下代码从互联网获取图像
- (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];
}
}
那么如何获得我长时间点击的图像? 我想将其保存到相册,谢谢。
对于您要尝试的操作,有一个更简单的解决方案。 我大概会这样
- (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
}
}
因此,基本上,这里发生的是将手势附加到Image而不是整个表视图,并且当手势在您的处理程序中发生时,可以访问调用该手势的UIImageView并可以从中获取Image。 您还可以将手势附加到整个单个单元(不确定用例是什么),并类似地提取ImageView和Image以供使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.