johnxnguyen / Down

Blazing fast Markdown / CommonMark rendering in Swift, built upon cmark.
Other
2.24k stars 319 forks source link

Loading an image with Styler #258

Open MrWoWander opened 3 years ago

MrWoWander commented 3 years ago

Added the ability to download an image from the markdown using the Styler class

codecov[bot] commented 3 years ago

Codecov Report

Merging #258 (2745a14) into master (bf24fcb) will decrease coverage by 7.13%. The diff coverage is 2.12%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #258      +/-   ##
==========================================
- Coverage   91.31%   84.17%   -7.14%     
==========================================
  Files          60       61       +1     
  Lines        1059     1150      +91     
==========================================
+ Hits          967      968       +1     
- Misses         92      182      +90     
Impacted Files Coverage Δ
...rces/Down/AST/Styling/Stylers/AsyncImageLoad.swift 0.00% <0.00%> (ø)
Sources/Down/AST/Styling/Stylers/DownStyler.swift 93.24% <22.22%> (-3.24%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update bf24fcb...2745a14. Read the comment docs.

MrWoWander commented 3 years ago

I cannot yet add testing, as Codecov asks for it, since I don’t understand how testing works in your project. This code works for me (in my project I inherited from the DownStyler class)

image

johnxnguyen commented 3 years ago

Hi @MrWoWander , thanks for the pull request! I'll take a look at this soon and get back to you.

MrWoWander commented 3 years ago

And the question arose: is it necessary to make a function that will automatically resize the image if it is larger than the device's resolution in width?

MrWoWander commented 3 years ago

For iOS, I did this:

extension UIImage {

    func scalePreservingAspectRatio(width: CGFloat) -> UIImage {
        let widthRatio = size.width / width
        let heightRatio = size.height / widthRatio

        // Compute the new image size that preserves aspect ratio
        let scaledImageSize = CGSize(
            width: width,
            height: heightRatio
        )

        // Draw and return the resized UIImage
        let renderer = UIGraphicsImageRenderer(
            size: scaledImageSize
        )

        let scaledImage = renderer.image { _ in
            self.draw(in: CGRect(
                origin: .zero,
                size: scaledImageSize
            ))
        }

        return scaledImage
    }
}

I personally use Down in conjunction with SwiftUI and while working in VStack, the picture crawls out of the edges of the screen. This method allows you to solve this problem.

MrWoWander commented 3 years ago

I also noticed that GIF images don't work. They are static. I'll try to come up with some solution

johnxnguyen commented 3 years ago

@MrWoWander sorry for the delay in reviewing this, it's been a busy past week. I'm planning to take a look at tonight.

johnxnguyen commented 3 years ago

And the question arose: is it necessary to make a function that will automatically resize the image if it is larger than the device's resolution in width?

I think it would be good to limit the size of the image to fit within the view that contains the string.

I cannot yet add testing, as Codecov asks for it, since I don’t understand how testing works in your project.

I use snapshot tests to assert the styling methods. In the case of images, you could probably add a test image and get the url of that resource. I think it would be good to have several tests to assert various cases of images, such as small images, large images, and invalid urls.

MrWoWander commented 3 years ago

@johnxnguyen, ok, I'll try in the coming days to think about replacing semaphore and handling URL requests, with the option to prohibit downloading and downloading from local paths

I will also add a reduction in the size of the images

johnxnguyen commented 3 years ago

@MrWoWander let me know if you need help, I'm happy to discuss ideas and problems together!

MrWoWander commented 3 years ago

@johnxnguyen It seems that asynchronous loading of the image should work while viewing markdown

Please check it out. If it works the way you wanted, then I will already make the error handler and the rest of the functionality that we discussed

MrWoWander commented 3 years ago

In some cases, I did not have time to download images in the SwiftUI view, so I added a delegate that allows me to update the view