NSImageView + Background Color - objective-c

I have requirement like this,
Have One NSIMageView and which will be changing frequently on the timer basis, On resizing, it needs to maintain the aspect ratio, so Image may not occupy the entire frame of the Image, in such case, i need to display the background color black,
can anyone help me how can i achieve that, On googling got one approach is to have SubClass of NSImageView, but no idea, what will impact on the performance, as image is changing frequently,

Set the image view's sizing mode to Proportionate. Enclose it in an NSBox set to Custom mode with a black background. No code needed, just a case of thinking inside the box...

Related

Specific behaviour wanted when loading and displaying an image in an NSScrollView (Mac OS X)

I am very unexperienced and rather new to Objective-C and I'd like to ask a methodical question. What I want to do is:
Load a user provided image to show on the screen. The user should be
able to zoom. So it seems best for me to use a NSScrollView to draw
the image. I have some other demands about the behavior
When the image is loaded, it should fit the NSScrollView without
being stretched in either direction.
When the user now changes the size of the NSScrollView (via changing
the size of the window) the image should be resized appropriately.
If the user at some point zooms the image, it shall not be resized
when changing the size of the NSScrollView. If the image is zoomed
to fit into the NSScrollView (means like when loaded) it should be
resized again when changing the size of the NSScrollView. I think
this is the same behavior as in Apple's preview app.
I guess this is easy to achieve for an experienced programmer, but I have some issues. Here's what I have tried.
I load the image via
[[NSImage alloc] initWithContentsOfFile:imagePathStr];
on a button click. I am also able that it is displayed to fit nicely (on load) into the NSScrollView by changing the size of the image before drawing it. But I can't get the behavior right on resizing or zoom (I use the NSScrollView biuld-in zoom ability). Can someone provide some kind of list of key points that needed to be executed to achieve what I want? Or do someone rather see my explicit code to tell me what I am doing wrong?

Too many UIViews causes lag

I am creating an app for practice that is a simple drawing app. The user drags his/her finger along the screen and it colors in a 100px x 100px square.
I currently achieve this by creating a new colored UIView where the user taps, and that is working. But, after a little time coloring in, there is substantial lag, which I believe is down to there being too many UIViews as a subview of the main view.
How can I, and others who similarly create UIViews on dragging a finger reduce the lag to none at all, no matter how many UIViews there are. I also think that perhaps this is an impossible task, so how else can someone like me color a cube of the size stated above in the main view on a finger dragged along the screen?
I know that this may seem like a specific question, but I believe that it could help others understand how to reduce lag if there are a very large amount of UIViews where a less performance reducing option is available.
One approach is to draw each square into an image and display that image, rather than keeping around an UIView for each square.
If your drawing is simple enough, though, you can use OpenGL to do this, which is much faster. You should look at Apple's GL Paint Sample Code which shows how to do this in OpenGL.
If your drawing is too complex for OpenGL, you could create, for example, a CGBitmapContext, and draw each square into that context when the user drags their finger. Whenever you draw a new square into that bitmap, you can turn the bitmap into an image (via CGBitmapConxtextCreateImage) and display that image an a UIImageView.
There are two things that come to my mind:
1- Use Instruments tool to check if you are leaking any memory
2- If you are just coloring the views than instead of creating images for each of them, either set the background color property of UIView or override the drawRect method to do custom drawing
I think what you are looking for is the drawRect: method of UIView. You could create your custom UIView (you propably have that already) and override the drawRect method and do your drawing there! You will have to save your drawings in an array or another container and call the setNeedsDisplay method whenever the array content is changed.

How can I place a small image in my UIView

I want to place a series of images in a UIView... I want to be able to change each image's color using three different .png files (red,yellow,green) as a status indicator. Other than actually drawing the rectangle and filling it, is there an easier way?
Research effort: I have Googled and looked through SO and found nothing dealing with this.
There are lots of ways you can do this. I think the easiest way would be to pragmatically add UIViews as subviews to your main view, or to your parent UIView.
You can set the frame of your views as well as the background color. See this link
adding uiview as a subview to the main view
You can also use images, but since it is a plain solid color, its a lot of extra storage space, etc. to use images when you can programatically render a color.
If it is a series of rectangles, you can store UIViews in some kind of data structure so it is easier to dynamically change the color at runtime. If you aren't moving them around, then init there frames/geometry so they are in the correct location, then access them as members of an array or something similar.
If you want to add gradients to images, this is the best thing I have found:
Gradients on UIView and UILabels On iPhone
I figured it out... I took a UIButton, changed the Type to "Custom", sized it (32 x 32) and programmatically changed the the image depending on circumstances.
[button setImage:<#(UIImage *)#> forState:<#(UIControlState)#>]
Thanks to both of you for getting the thought process running... :D

Too small annotation iOS

I have made an application which simulates movement of buses on a map.
The buses are MKAnnotations moving atop a MKMapview. By touching the annotation/bus, it will show a custom made callout with info about the bus. It is all based upon public realtime data.
A problem I have is that the MKAnnotations (buses) are very small, and since they are constantly moving, it is often hard to touch the annotation to show the callout.
The icon is 25x25 pixels, and can not be increased.
Is there any way to make an invisible rect which will increase the touch-sensitive area around an annotation?
Create a larger annotation view and set the background color to [UIColor clearColor].
Create a 25x25 UIImageView and add it to the annotation view. You can set the frame of the ImageView to place it in the center of the annotation view.
I ended up doing it non-programmatically.
Using photoshop, I made a 75x75pix 100% opaque picture, and added my 25x25pix icon on top.
Maybe not the best solution, but it worked.

Resize NSTableView or NSScrollView depending on number of rows in table

I have a view-based NSTableView which is embedded in an NSScrollView. It has custom cells that are x number of pixels high. The NSScrollView is the same size as the panel that it is a subview of. I want to resize the entire NSTableView depending on how many rows are in the table.
Everything is working except the resizing. Resizing the scroll view manually in IB seems to have the desired affect, but NSSrollView does not seem to have a class method to resize its view (like NSView has setFrame). Should I be resizing the scollview, the tableview, both, or something else? Does NSScrollView have a setFrame method or similar that I am missing?
Thanks.
Before you try to do it programmatically, make sure you have the outline view's autosizing masks set up properly in the nib file. It sounds like you simply want the outline view (and its scroll view) to always remain the same size as the window that it's inside.
By default, the autosizing masks of an NSScrollView/NSOutlineView combo that you place into a window looks like the following:
In other words, it's set up to always remain the same size as it is now, no matter how large you resize the window to be.
What you want to do is to change the autosizing masks to look like in the image below:
To do that, you click in the white autosizing box wherever there's a dotted red line to toggle it into a solid red line. Once it's configured that way, the scroll view (and table view) will always (automatically) be resized to be the same size as the window that it's in.
There may also be a way to achieve this using Lion's new "auto layout" feature, but I'll have to leave that to someone who has more experience with it.
In case you really need to do this (such as when you want all rows to fit in the scrollview alleviating the need to scroll) and the scroll view is only a portion of the window/view you can do:
[[myTableView enclosingScrollView] setFrame:newFrameRect];
scrollview.frame = CGRrectMake(x, y, w, h);