I'm doing some drawing on pdfs, and I've found what may be a problem on Xcode handling of pdf dimensions, which then become clear when CGPDFDocumentCreateWithURL is triggered.
I have two PDFs. One is A4 portrait size (612w x 792h) and it displays fine. The other one is closer to A4 landscape, 842w x 595h. While it displays in the UIScrollview I'm using, the vertical offset of page height (pageRect.size.height) in CGContextTranslateCTM caused it to move too far down the view. I NSlogged pageRect dimensions and found that the height and width had been flipped. I then checked back to the pdf's listing in supporting documents, and while clearly displaying the pdf as landscape, the dimensions were shown as 595x842, whereas the A4 portrait was correctly dimensioned as 612x792.
Has anyone else hit this problem, and is there a fix.
The link to the original landscape file is here.
You can create landscape pages in 2 ways:
set the width to 842 and height to 595 or
set the width to 595 and height to 842 and rotate the page with 90/270 degrees.
In your situation I assume you have a rotated portrait page (2).
After you get the page width and height you also have to get the page rotation and if the rotation is 90/270 you have to swap the width/height.
You also have to consider the origin of the coordinate system. When the page is not rotated the origin is in the visual bottom left corner of the page, visually X grows from left to right, Y grows from bottom to top. When the page is rotated with 90 degrees, the origin is in the visual top left corner, visually X grows from top to bottom, Y grows from left to right.
The actual PDF coordinate system does not change when the page is rotated, what changes is how to PDF coodinate system is mapped to what you actually see on the screen, this is what 'visually' refers to.
Related
We have a typical bootstrap SPA that we turned into a React site.
In one of the sections there's an image that appears in the right side of the layout, and it spans the section vertically (takes up about 50% horizontally).
When the window is reduced in size, at some intermediary positions, the image no longer spans the section vertically, making it float above what looks like a margin or a frame, but in fact, it's just the background color of the section whose aspect ratio is not enough to hold the image completely.
Is there a way to prevent this? It seems like the only solution would be to pick images that are have aspect ratios that are more amenable to the half the grid position they are being given.
Here is my problem.
I have a square image which does not fit on an iPad or iPhone app, as the screens are not square, so i decided to add a UIImageView which:
When the Phone is on Portrait position i add a constraint to the top (-20) and bottom (0) to fill the whole screen aligning X in the center and keeping the aspect ratio to keep the image square even if the screen is bigger or smaller, this means that in both sides i will "loose" part of my image, but that is fine.
When I rotate my phone to landscape position, the screen is widder,so i see a square image in the middle of the screen but a white rectangle in both sides.
I think the sollution to this is when it is on landscape position i need to remove the top and bottom constraints and add one to lead (-20) and one to trail (-20), center the image on Y axis and keep the 1:1 ratio...
But my question is: how to add a constraint to valid for one orientarion and change it to the other?
What is the best approach?
thanks
FP
You don't need to do it programmatically. I recommend you make the view fill the superview -- have one constraint each to top, bottom, left, and right. Then for the UIImageView attributes inspector in Interface Builder, set "View Mode" to Aspect Fill.
I think you have it the wrong way around.
While on portrait you should set trail and lead constraints as your height is bigger than the width, so if you set (0) for trail and lead and 1:1 ratio you are safe that it will fit beautifully. Also center it vertically in container.
On landscape, as the width is bigger than the height, you should set top and bottom (0) and ratio 1:1 and center it horizontally in container.
i have some picture which is 675x503. I would like to make more width on it and let's say to 1024 and in height to 400. How to do that without cutting my image and keep quality? I just would like to have this image on my website top. I got photoshop 6. I tried with Image->image size but its not what i need.
If you don't want to cut the picture at all, this will distort the image due to different height and width ratios. If this is not an issue, you could simply hit ctrl(or cmd)+t when the layer with the picture is selected. It will let you resize and rotate the entire image. There is also an option in the Image -> Image Size menu called "Bicubic Sharper", found in the drop-down list at the bottom of the menu(Photoshop CS6). It's meant for image reduction and should also solve your problem.
I am trying to render the image of a scroll view even to the areas which is outside the visible area of the screen. I am resizing the frame of the scroll view to include the content size width and height.After resizing, I am doing a renderInContext and finally taking the image by UIGraphicsGetImageFromCurrentImageContext.
All I am getting is a square box of black color of certain bytes. If i do not resize the frame of the view, i get the image with incorrect frame.
Does changing the frame affect renderInContext? What can i do to get the correct image after resizing?
I'm wondering what the dropdown "Mode" is about? It contains "Scale to fill", "Aspect fit" and so on. I never had to change it so far, still I'm curious what it can be used for. Can somebody explain?
The content mode property of a view tells how its content should be laid out. In the Interface Builder, the various modes can be selected in the Attributes Inspector.
Let's use two image views to see how the various modes work.
Scale to Fill
The image heights and widths are stretched to match the size of the UIImageView.
Aspect Fit
The longest side (either height or width) of the image is stretched to match the view. This makes the image as big as possible while still showing the entire image and not distorting the height or width. (I set the UIImageView background to blue so that its size is clear.)
Aspect Fill
The shortest side (either height or width) of the image is stretched to match the view. Like "Aspect Fit", the proportions of the image are not distorted from their original aspect ratio.
Redraw
Redraw is only for custom views that need to do their own scaling and resizing. We aren't using a custom view, so we shouldn't use Redraw. Notice that here UIImageView just gives us the same result as Scale to Fill, but it is doing more work behind the scenes.
About Redraw, the documentation says:
Content modes are good for recycling the contents of your view, but you can also set the content mode to the UIViewContentModeRedraw value when you specifically want your custom views to redraw themselves during scaling and resizing operations. Setting your view’s content mode to this value forces the system to call your view’s drawRect: method in response to geometry changes. In general, you should avoid using this value whenever possible, and you should certainly not use it with the standard system views.
Center
The image is centered in the view, but the length and width of the image are not stretched.
Top
The top edge of the image is centered horizontally at the top of the view, and the length and width of the image are not stretched.
Bottom
The bottom edge of the image is centered horizontally at the bottom of the view, and the length and width of the image are not stretched.
Left
The left edge of the image is centered vertically at the left of the view, and the length and width of the image are not stretched.
Right
The right edge of the image is centered vertically at the right of the view, and the length and width of the image are not stretched.
Top Left
The top left corner of the image is placed at the top left corner of the view. The length and width of the image are not stretched.
Top Right
The top right corner of the image is placed at the top right corner of the view. The length and width of the image are not stretched.
Bottom Left
The bottom left corner of the image is placed at the bottom left corner of the view. The length and width of the image are not stretched.
Bottom Right
The bottom right corner of the image is placed at the bottom right corner of the view. The length and width of the image are not stretched.
Notes
If the content (in our case the image) is the same size as the view (in our case the UIImageView), then changing the content mode will make no noticeable difference.
See this and this question for a discussion about content modes for views other than UIImageView.
In Swift, to set to set the content mode programmatically you do the following:
imageView.contentMode = UIViewContentMode.ScaleToFill
imageView.contentMode = UIViewContentMode.ScaleAspectFit
imageView.contentMode = UIViewContentMode.ScaleAspectFill
imageView.contentMode = UIViewContentMode.Redraw
imageView.contentMode = UIViewContentMode.Center
imageView.contentMode = UIViewContentMode.Top
imageView.contentMode = UIViewContentMode.Bottom
imageView.contentMode = UIViewContentMode.Left
imageView.contentMode = UIViewContentMode.Right
imageView.contentMode = UIViewContentMode.TopLeft
imageView.contentMode = UIViewContentMode.TopRight
imageView.contentMode = UIViewContentMode.BottomLeft
imageView.contentMode = UIViewContentMode.BottomRight
View Programming Guide goes into details of what you're asking about. If you scroll down to the section called "Content Modes" you'd find what you're looking for.
Basically according to Apple:
"Each view has a content mode that controls how the view recycles its content in response to changes in the view’s geometry [...] the value in the contentMode property determines whether the bitmap should be scaled to fit the new bounds or simply pinned to one corner or edge of the view."
http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
should give you the basic ideas very well.