Does changing the frame of the view affect renderInContext and UIGraphicsGetImageFromCurrentImageContext? - ios7

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?

Related

Apply Image in Buttons

As A Begineer I've made a Puzzle game and it's working fine in Iphone Simulators.
But problem occurred when I run it in bigger Screen like IPad Air 2.All the picture aren't fitting perfectly in the buttons
(Note that Images are applied on buttons not in the Background of
buttons)
As image size not fit to given size of button, may be due to size concern of aspect fit property of imageview of button. You can try to scale image proportionally to size of imageview of button and get desire result.
You can refer to this link for scale image as proportionally to desired size.
scale Image in an UIButton to AspectFit?

Cocoa - screen capture and draw (retina)

I`m try get screen capture from retina display, and draw this image on window.
I can get capture
CGDirectDisplayID displayId = CGMainDisplayID()
CGImage imageRef = CGDisplayCreateImage(displayId)
image have a size 2560x1600
Now, i need draw this image on window. But if, i resize window to full screen, window return size 1280x800. If i wont draw image on window, i need scale image to 1280x800. Image lost quality, this is not good.
Please help me, how i can get screen capture from retina display and draw his on window, without lost quality ?
You can find out actual window size in pixels by passing its frame to this method:
-[NSWindow convertRectToBacking:]
Just draw the image to the view's bounds. In theory this is a scaling operation, but in reality it's not because the view's bounds are in points, not pixels. The backing buffer for the view is at the same DPI as the original screen capture, so it will be a straight blit. No detail will be lost.

NSImageView won't scale image down if frame style is NSImageFrameNone

I need to display NSImageView on resizable NSWindow. The image displayed in the view should scale down if it's too big to fit image view in the widnow and it should also change its size when resizing the window. I am able to achieve correct behavior using Auto-Layout, and setting imageScaling property of NSImageView to NSImageScaleProportionallyDown. Unfortunately it's not working when my image view's imageFrameStyle property is set to NSImageFrameNone (it works for any other option like NSImageFramePhoto or NSImageFrameGrayBezel). I don't want any frame to be displayed, like with NSImageFrameNone, but disabling the frame breaks autoresizing - it looks like with frame set to NSImageFrameNone image is not scaled down and NSImageView is scaled up to match displayed image size. Anyone have solution for that issue?
It occurs that it's easier than I thought. The problem was with Auto-Layout, not with NSImageView scaling logic. Using InterfaceBuilder with my image view selected I had to change "Content Compression Resistance Priority" to lower value (it's located on Size Inspector tab in Xcode5). That solved my problem and the image is now scaled properly when resizing window.

What is the "Mode" property in Interface Builder which offers "Scale to fill", "Aspect fit" etc.?

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.

Make NSView resizable WITHOUT resizing images contained within

I have an NSView that resizes automatically based on the size of the window. I load images and display them in the NSView, but I do not want the NSView to try and "squeeze" them to fit the NSView's size. What I would like is to have the images load into the NSView at 100%, then resize the window to reveal more of the image rather than scale it to fit. I may want to modify this later to allow zooming and panning as well.
Here's what I did:
-(void)drawRect:(NSRect)rect
{
NSRect theRect = NSMakeRect((rect.size.width/2)-(([theImage pixelsWide]/2)/2),
(rect.size.height/2)-(([theImage pixelsHigh]/2)/2),
[theImage pixelsWide]/2,
[theImage pixelsHigh]/2);
[theImage drawInRect:theRect];
}
How are you displaying the images in the first place? Either you're using an NSImageView or you're drawing the images into your custom view yourself in -drawRect:.
If you're using an image view, take a look at its autosizing settings (its geometry as its parent view's geometry changes) and its image scaling properties (the image size and ratio as the image view's geometry changes.
If a custom view then draw the image into whatever rectangle you please rather than the view's entire bounds.