iPhone 如何将 pdf 文件作为图像应用到 table 视图单元格
iPhone how can i apply a pdf file as image on a table view cell
我正在做一个项目,我必须在 table 视图单元格上放置图像。
是否可以将 .pdf 格式文件放在 table view cells as image 上?
我使用了以下代码:
- (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;
}
是的,您当然可以,但它只会给您 PDF 第一页的截图……几乎就像缩略图一样。操作方法如下:
- 在 Xcode 中创建资产文件夹:
- 将 pdf 文件放入资产目录:
- 更改图片使其格式正确,请看这张图片的右侧,您必须将侧面的第 5 个下拉菜单更改为 "single vector":
- 将图片拖到正确的插槽中,在上一张图片中看到黄色警告标记了吗?发生这种情况是因为图像不在正确的位置,您必须将 PDF 文件拖到上面的位置才能更正此问题:
您可以像这样使用此 PDF 矢量文件:
[_window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"examplepdf"]]];
我只在我的应用程序中使用矢量化 pdf。系统会自行将这些调整为正确的 iphone 和 ipad 大小,无需使用 @2x 或 @3x 或其他任何东西。
这是一个关于其工作原理的附加教程:
http://martiancraft.com/blog/2014/09/vector-images-xcode6/
您真的应该首先按照上面教程中的说明在 Illustrator 中保存 PDF 文件,但无论哪种方式,此方法都应该有效。祝你好运!
这是我刚刚在上面解释的小教程的输出:
您可以使用 CoreGraphics 绘制 PDF:
- 使用
CGPDFDocumentCreateWithURL()
打开 PDF。
- 使用
CGPDFDocumentGetPage()
获取(第一页或任何其他)页面。
- 使用
CGContextDrawPDFPage()
将页面绘制到您自己的上下文中。
您需要有绘图上下文,所以:
- 创建
UIView
子类,覆盖 -drawRect:
并使用它代替 UIImageView
。
- 使用
UIGraphicsBeginImageContextWithOptions ()
设置图像上下文,这样您将获得一个 UIImage
实例。
我正在做一个项目,我必须在 table 视图单元格上放置图像。 是否可以将 .pdf 格式文件放在 table view cells as image 上?
我使用了以下代码:
- (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;
}
是的,您当然可以,但它只会给您 PDF 第一页的截图……几乎就像缩略图一样。操作方法如下:
- 在 Xcode 中创建资产文件夹:
- 将 pdf 文件放入资产目录:
- 更改图片使其格式正确,请看这张图片的右侧,您必须将侧面的第 5 个下拉菜单更改为 "single vector":
- 将图片拖到正确的插槽中,在上一张图片中看到黄色警告标记了吗?发生这种情况是因为图像不在正确的位置,您必须将 PDF 文件拖到上面的位置才能更正此问题:
您可以像这样使用此 PDF 矢量文件:
[_window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"examplepdf"]]];
我只在我的应用程序中使用矢量化 pdf。系统会自行将这些调整为正确的 iphone 和 ipad 大小,无需使用 @2x 或 @3x 或其他任何东西。
这是一个关于其工作原理的附加教程:
http://martiancraft.com/blog/2014/09/vector-images-xcode6/
您真的应该首先按照上面教程中的说明在 Illustrator 中保存 PDF 文件,但无论哪种方式,此方法都应该有效。祝你好运!
这是我刚刚在上面解释的小教程的输出:
您可以使用 CoreGraphics 绘制 PDF:
- 使用
CGPDFDocumentCreateWithURL()
打开 PDF。 - 使用
CGPDFDocumentGetPage()
获取(第一页或任何其他)页面。 - 使用
CGContextDrawPDFPage()
将页面绘制到您自己的上下文中。
您需要有绘图上下文,所以:
- 创建
UIView
子类,覆盖-drawRect:
并使用它代替UIImageView
。 - 使用
UIGraphicsBeginImageContextWithOptions ()
设置图像上下文,这样您将获得一个UIImage
实例。