Can you create an UIImage from PDF data? - ios

I am loading a remote PDF file and trying to turn it into an UIImage (for showing in an UIImageView), and am surprised that it doesn't seem to work. Even though local PDF files in the Asset Catalog work just fine, is PDF not supported outside of that?
Edit: I found https://developer.apple.com/library/archive/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/LoadingImages/LoadingImages.html which suggests that nope, PDF doesn't work. So that leaves me with the question: why/how does it work for local files in the Asset Catalog? Why or how is that different?

To answer your "Why or how is that different?" question...
When you add a PDF file to asset catalog, Xcode renders #1x, #2x and #3x versions at build-time.
You can select Preserve Vector Data to have the pdf "rendered on-the-fly." This generally works well, although if you have a lot of pdfs and you're doing a lot of manipulation with them, it may reduce performance.

Related

Is it obliged to use Assets.xcassets?

I would like to know if we were to put our images in the xcode Assets.xcasset or an image file could be enough?
Thank you
https://developer.apple.com/videos/play/wwdc2018/219/
From WWDC 2018:
"...for artwork that comes with your application, we strongly encourage you to use image assets. And there are a number of reasons why."
Some of those reasons:
"Image assets are optimized for name based and trait-based lookup. It's faster to look up an image asset in the asset catalog, than it is to search for files on disk that have a certain naming scheme."
"managing buffer sizes"
"...per device thinning, which mean that your application only downloads image resources that are relevant to the device that it's going to run on and vector artwork."
Vector artwork optimization: "...if your image gets rendered in an image view that is larger or smaller than the native size of the image it doesn't get blurry. The image is, actually, re-rasterized from the vector artwork... The asset catalog compiler has, actually, already produced a pre-rasterized version of that image and stored it in the asset catalog. So, rather than doing the complicated math of rasterizing your vector artwork into a bitmap, we can just decode that image that's stored in the asset catalog and render it directly into the frame buffer."
This is not an exhaustive list, click the link for more. So to answer the question, using the asset catalog is certainly not required, but it would be foolish not to use it for artwork that is delivered with the application for the reasons mentioned.
No. It is not required to use .xcassets files. You can also just add images and other resources to your Xcode project.
However I would always use Asset Catalogs if possible, because it gives you powerful features, such as using different images for different devices/gamuts/etc.

PDF image for iOS Assets

we can add image as pdf in ios assets for application.But i'm confusing that which approach is good for application.
Png file direct drag and drop to Assets
or use image as PDF
i have follows links
https://icons8.com/articles/how-to-use-vectors-in-xcode-7/
and https://bjango.com/articles/idontusepdfs/
but not able to decide which one is better solution for assets.
i know how to works with pdf asset but cannot decide which method is better from development point of view.
Thanks in advance.
If your artwork has complex gradients, generate the PNGs yourself. Otherwise, use PDFs. That's how I read it.
Personally, I have the luxury of working with a designer who just gives me assets at all the required sizes. On another project, Sketch was used which can auto-generate PNGs from a script as part of the build process. So I've never actually used PDFs.

Image Localisation using images in Assets

I found the following issue exists for a long time, And I also find there are many solutions available, But this thread is to get recommended way to do it.
Issue:
We cannot able to localise the images directly from assets folder but we can able to localise the images which we place it in File inspector as shown in the image.
I need to know the answers for the following questions:
- Why we don't have localisation for images in Assets folder ?
- What is the Apple recommended way to localise images ?
Please answer the questions. And if you have any link recommended by Apple please add that with your answers.
Note:
- Its not a duplicate question, am asking this since we know we cannot able to extract / reverse engineer to get the images used in the iOS application assets. But thats not the case with the images placed in the file inspector which can be extracted. So this question is all about Apple's recommended way to approach image localisation and not possible solution.
There is not any correct way to localise the xcassets file directly,
but still you can localize the name of the asset to get using your localizable.strings file.
For example:- you want to put any main image at home screen for different different region so you can save that image to xcassets to using their localise name and you can fetch that image like this.
UIImage *img = [UIImage imageNamed:NSLocalizedString(#"Main_image", nil)];
and here is the link from apple that spacify how you can localize the image. LINK

Generating a docx file using Pandoc: images missing! Due to multiple requests?

I'm generating a markdown document using my Rails 4.2 app which includes images that are on the same server (in the public folder).
Using pandoc (pandoc-ruby 1.0.0), I want to convert the document into various formats, especially HTML (to preview it in the browser) and DOCX (to download it).
The preview in the browser works perfect. But when converting to DOCX, the images aren't included. I guess this is due to multiple requests to referenced images while pandoc is generating the document.
I have already experimented with setting allow_concurrency to true, but this didn't solve the problem. Also, it happens on both the development and the production environment (while in development, it takes a long time, and in production it doesn't - maybe due to some differences in timeout limits?).
I have already found a way to solve my problem by not referencing the images using an URL, but by embedding it as base64 string into the document. But this for sure can't be the solution of choice, as it tends to bloat up the HTML document a lot. Also, on production, I already get RuntimeError (Stack space overflow: current size 8388608 bytes) from pretty small embedded images. So I have to find a real solution.
Reference the images by file path instead of url if they are on the same server.

optimze pdf file size in rails app

For a rails app that works a lot with uploaded image heavy pdf files I'm looking for a way to optimize the file size of uploaded pdf's.
Adobe Acrobat has a 'save as reduced file size pdf' option which often halves the filesize when images are included.
I would like to do a similar action that is triggered after a file upload in my rails app.
Any ideas?
While #lzap's comment may be true, if you still want to give it a shot, you might look at pdftk (PDF Toolkit). Its a library for manipulating and creating PDF files that looks like it offers the ability to compress a given pdf file.
The library can be installed on most major operating systems, so if you have the ability to install it on your host, then simply call:
system("pdftk uncompressed-input.pdf output compressed-outpu.pdf compress")
inside your rails app whenever you want to compress a particular PDF file. I have no idea how long this would take, and if you are compressing many PDF's at the same time, you may want to consider handing off to a background job (without this, Rails will wait until the compression is done before returning anything to the browser, probably causing a timeout error for long running groups of compression calls).
Also, if your file names come from user input, be extra careful to avoid injection attacks.

Resources