简体   繁体   English

iPhone如何在表格视图单元格上将pdf文件作为图像应用

[英]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: 这是您的操作方式:

  1. Create an assets folder in Xcode: 在Xcode中创建资产文件夹:

在此处输入图片说明

  1. Place pdf files into assets catalog: 将pdf文件放入资产目录:

在此处输入图片说明

  1. Change the image so it's the correct format, look on the right side of this picture, you have to change the 5th drop down on the side to "single vector": 更改图像以使其是正确的格式,请看这张图片的右侧,您必须将侧面的第5个下拉菜单更改为“单个矢量”:

在此处输入图片说明

  1. Drag image into correct slot, in the previous image you see the yellow warning marker? 将图像拖到正确的插槽中,在上一个图像中您会看到黄色警告标记? This happens because the image isn't in the correct slot, you have to drag the PDF file to the slot above to correct this: 发生这种情况是因为图像不在正确的插槽中,您必须将PDF文件拖到上方的插槽中才能更正此问题:

在此处输入图片说明

  1. 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:

  1. Open the PDF using CGPDFDocumentCreateWithURL() . 使用CGPDFDocumentCreateWithURL()打开PDF。
  2. Get the (first or any other) page using CGPDFDocumentGetPage() . 使用CGPDFDocumentGetPage()获取(第一个或任何其他)页面。
  3. Draw the page into your own context with CGContextDrawPDFPage() . 使用CGContextDrawPDFPage()将页面绘制到您自己的上下文中。

You need to have the drawing context, so either: 您需要具有绘图上下文,因此:

  • Create UIView subclass, override -drawRect: and use it instead of UIImageView . 创建UIView子类,重写-drawRect:并使用它代替UIImageView
  • Setup an image context using UIGraphicsBeginImageContextWithOptions () so you will get an UIImage instance. 使用UIGraphicsBeginImageContextWithOptions ()设置图像上下文,以便获得UIImage实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM