当点击iOS SDK中的单元格时,如何从tableviewcell获取图像(使用CGContext) [英] How to get image( using CGContext ) from the tableviewcell when tapped on the cell in iOS SDK
问题描述
如何使用与我点击的tableviewcell相同的CGContext进行成像。我得到了我拍摄的细胞框架。我看过很多CGContext的例子,它只把大小作为输入并将图像转换成那个大小。
How to get imaged using CGContext same as the tableviewcell that I Tapped. I got the frame of the cell which I tapped. I had seen many examples of CGContext it only takes the size as the input and converting the image into that size.
但我想得到tableviewcell的图像,因为它具有相同的框架。任何人都可以建议我怎样才能做到这一点。
But I want to get the Image of the tableviewcell as it is with the same frame. Can anyone suggest me how can I do this.
推荐答案
我猜你需要在你的手机后的适当位置加入这两个逻辑正在被挖掘。
有一个方法 - (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;
来获取 CGRect
当前单元格。
I guess You need to club these two logic in appropriate place after your cell is being tapped.
There is a method - (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;
to get the CGRect
of your current cell.
- 查找
CGRect
> UITableViewCell 被点击。 -
从
获取
。CGSzie
步骤1的CGRect
- Find the
CGRect
of theUITableViewCell
being clicked. Get the
CGSzie
from theCGRect
of step 1.
-(UIImage *) screenshot:(CGRect )cellRect onCell:(UITableViewCell)cell
{
UIGraphicsBeginImageContext(cellRect.size);
CGContextRef context=UIGraphicsGetCurrentContext();
[cell.layer renderInContext:context];
UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
这应该可以解决你的问题问题,可能需要一些调整。
This should solve your problem, may need a few tweaks.
这是用法:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIImageView *imageV = [[UIImageView alloc]initWithImage:[self screenshot:[tableView rectForRowAtIndexPath:indexPath] onCell:[tableView cellForRowAtIndexPath:indexPath]]];
imageV.frame = CGRectMake(0, 75, self.tableView.frame.size.width,100);
UIViewController *viewController = [[UIViewController alloc]init];
[viewController.view addSubview:imageV];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:viewController];
[self presentViewController:nav animated:YES completion:^{
}];
}
屏幕:选定的第1行
将图像视图加载到新视图控制器上
Loaded the image view onto new view controller
这篇关于当点击iOS SDK中的单元格时,如何从tableviewcell获取图像(使用CGContext)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!