Skip to content

Maximum image size #272

Open
Open

Description

@marcheimendinger

It may be possible but I didn't find how to do it.

How should the feature work?

I'd like to set the maximum size (height, width, or both) for PDFImage in a document while preserving aspect ratio.

This could look like this:

let image = UIImage(named: "awesome-image")!
let imageElement = PDFImage(image: image)
imageElement.maximumSize = CGSize(width: 100, height: 100) // If the image is not a square, width OR height will be 100 (or smaller if impossible to fit) and the other smaller
document.add(image: imageElement)

Why do I want this?

The idea is to generate a mosaic of images (with captions) using imagesInRow. I want images to resize but not to the whole free available space. For example if I only have one image, I don't want it to take the whole page width. And if I specifically set the image size, the image gets cropped to this size which I don't want either.

I'm not using a table for this purpose as each image can have a different size and I want them to keep their original aspect ratio, which table doesn't seem to support at the moment (I also noticed some strange behaviour with images in table, where images are upside down or 90° rotated).

TPPDF Environment

TPPDF version: 2.3.5

Activity

  1. parthmodi605 commented on Jan 5, 2022

    @parthmodi605

    I have created a solution for this problem by adding empty images

    // first get real data from image array and convert it to PDFImage

     var pdfImagesArr = [PDFImage]()
            for data in otherImageArr{
                let imageData = data["image"] as? Data ?? Data()
                let text = data["name"] as? String ?? ""
                let fetchImage = UIImage(data: imageData) ?? UIImage()
                let otherImage = PDFImage(image: fetchImage.resized(withPercentage: 0.4) ?? UIImage(),caption: PDFAttributedText(text: NSAttributedString(string: text == "" ? " " : text ,attributes: captionAttributes)), quality: 1.0, options: .none)
                pdfImagesArr.append(otherImage)
            }
    

    // once we get converted PDFImage array, we will add empty images to get 3 images per row like we have 1 image to show we need to add +2 empty images

            if pdfImagesArr.count % 3 != 0{
                var result = 0
                var diffCellCount = 0
                result = pdfImagesArr.count % 3
                if result != 0{
                    diffCellCount = 3 - result
                    for _ in 0..<diffCellCount{
                        let otherImage = PDFImage(image: UIImage(named: "Empty") ?? UIImage(),caption: PDFAttributedText(text: NSAttributedString(string: "",attributes: captionAttributes)))
                        pdfImagesArr.append(otherImage)
                    }
                }
            }
    

    // setting 3 images per row

            var threeImages = [PDFImage]()
            for data in pdfImagesArr{
                threeImages.append(data)
                if threeImages.count == 3{
                    document.add(imagesInRow: threeImages, spacing: 10)
                    document.add(space: 20)
                    threeImages.removeAll()
                }
            }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions