[英]iPhone how can i apply a pdf file as image on a table view cell
I am working on a project in which I have to put images on table view cells. 我正在一个项目中,必须将图像放在表格视图单元格上。 Is it possible to put .pdf format files on table view cells as image?
是否可以将.pdf格式的文件作为图像放在表格视图单元格上?
I used the following code: 我使用以下代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// NSDictionary *menuAttr = [menuItems objectAtIndex:indexPath.row];
// cell.textLabel.text = [NSString stringWithFormat:@"%@", [menuAttr objectForKey:@"name"]];
if (indexPath.row ==0 )
{
cell.imageView.image = [UIImage imageNamed:@"about us.pdf"];
cell.imageView.layer.cornerRadius =cell.imageView.image.size.width/2;
}
if (indexPath.row == 3)
{
cell.imageView.image = [UIImage imageNamed:@"policies.pdf"];
}
if (indexPath.row == 2)
{
cell.imageView.image = [UIImage imageNamed:@"services.pdf"];
}
if (indexPath.row == 1)
{
cell.imageView.image = [UIImage imageNamed:@"home.pdf"];
}
if (indexPath.row == 4)
{
cell.imageView.image = [UIImage imageNamed:@"contact us.pdf"];
}
if (indexPath.row == 5)
{
cell.imageView.image = [UIImage imageNamed:@"feedback.pdf"];
}
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
cell.backgroundColor = [UIColor clearColor];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.contentView.backgroundColor = [UIColor clearColor];
return cell;
}
Yes, you sure can, but it's only going to give you a shot of the first page of the PDF ... almost like a thumbnail. 是的,您当然可以,但这只会为您提供PDF第一页的快照……几乎就像缩略图一样。 Here's how you do it:
这是您的操作方式:
you can use this PDF vector file like this: 您可以使用以下PDF矢量文件:
[_window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"examplepdf"]]];
I only use vectorized pdfs in my apps. 我只在我的应用程序中使用矢量化pdf。 The system resizes these for the correct iphone and ipad sizes by itself, no need to use @2x or @3x or anything else.
系统自行调整这些大小以适合正确的iphone和ipad大小,无需使用@ 2x或@ 3x或其他任何格式。
Here's an additional tutorial on how this works: 这是有关其工作原理的其他教程:
http://martiancraft.com/blog/2014/09/vector-images-xcode6/ http://martiancraft.com/blog/2014/09/vector-images-xcode6/
You really should FIRST save the PDF files in Illustrator as explained in the tutorial above, but either way, this method should work. 您确实应该按照上面的教程中的说明先将PDF文件保存在Illustrator中,但是无论哪种方式,这种方法都应该起作用。 Good luck!
祝好运!
Here's the output from the small tutorial I just explained above: 这是我上面刚刚解释的小教程的输出:
You can draw PDFs using CoreGraphics: 您可以使用CoreGraphics绘制PDF:
CGPDFDocumentCreateWithURL()
. CGPDFDocumentCreateWithURL()
打开PDF。 CGPDFDocumentGetPage()
. CGPDFDocumentGetPage()
获取(第一个或任何其他)页面。 CGContextDrawPDFPage()
. CGContextDrawPDFPage()
将页面绘制到您自己的上下文中。 You need to have the drawing context, so either: 您需要具有绘图上下文,因此:
UIView
subclass, override -drawRect:
and use it instead of UIImageView
. UIView
子类,重写-drawRect:
并使用它代替UIImageView
。 UIGraphicsBeginImageContextWithOptions ()
so you will get an UIImage
instance. UIGraphicsBeginImageContextWithOptions ()
设置图像上下文,以便获得UIImage
实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.