Hello,
I wanted to use different images for each screen resolutions (iPhone & iPad), and I discovered that I can use PDF files for images in assets, so I did it. Here the configuration of my asset :
I wan’t if I use the correct way or if I can do it easier. Per exemple, I have a button positioned at the center of the screen (In this case, the code does not interest us) :
let buttonWidth = self.size.width * 0.5
let buttonHeight = buttonWidth * 0.1
button = UIButton(type: UIButtonType.Custom) as UIButton
button.frame = CGRectMake(0, 0, buttonWidth, buttonHeight)
button.setBackgroundImage(UIImage(named: "buttonImageInitial"), forState: UIControlState.Normal)
self.view?.addSubview(button)
To know the button image size, I use self.size.width as the width of iPhone 4S screen (640) and I get the button width for the iPhone 4S : 640 * 0.5 = 320, and the height : 320 * 0.1 = 32. So the size of my button is : width: 320, height: 32. In Photoshop I create an image with these dimensions (in Points) and I export it in PDF. I add to the asset catalog as the iPhone button image (I think Xcode convert Points into Pixels, because in the Attribute Inspector, the size of the image is “320 x 32 pixels”).
For the iPap dimensions, I do the same, but I don’t use self.size.width as the width of the iPhone 4S screen, but as the width of iPad screen (1536), and I get 768 * 76,8 for the size of the button.
My questions are : Did I use the right way, can I use Universal in Devices to make only one image for the button, and how to choose the only correct size for each screen resolution ?
My Xcode version : Xcode 7.0 beta 5
Thanks !
2
Answers
I am not completely sure i understand your question, but it looks like you are using it in the correct way.
When you add a PDF file to Xcode (this goes for iOS only), it treats it as a 1x asset, and then at build time creates rasterized (PNG) assets for all scales (1x, 2x, 3x). So by the time your code runs, the asset isn’t vector anymore – it’s just a bitmap. But Xcode only does this rasterization to 1x, 2x and 3x, and for that it takes into consideration the size of the asset specified in the PDF. If your PDF specifies the size of the button to be, for example, 160×32, then Xcode will render it at: 160×32 (1x), 320×64 (2x) and 480×96 (3x).
In other words, there is no way currently to tell Xcode to rasterize your PDF at different sizes you may need, which sounds like what you are trying to do.
In the attributes inspector in Xcode 8, choosing “single scale” from the “scales” dropdown (I’m guessing “scale factors” in your screenshot) will treat the asset as universal.