get the width and height of NSView and NSImageView - cocoa

I have a NSView, there is a NSImageView on the NSView
in the source codes of the NSView
I wrote:
NSImage *image = [[[NSImage alloc] initWithContentsOfURL:url] autorelease];
NSImageView *newImageView = nil;
newImageView = [[NSImageView alloc] initWithFrame:[self bounds]];
[newImageView setImage:image];
[newImageView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
Is there a way to read the width and height of the NSView and NSImageView?

The size of any view (NSView, NSImageView, etc.) is view.frame.size or view.bounds.size.
In both cases it is an identical NSSize struct. You can write code like this:
NSSize size = newImageView.frame.size;
NSLog(#"size: %#", NSStringFromSize(size));
NSLog(#"size: %f x %f", size.width, size.height);
To change it, you need to update the view frame property:
NSRect frame = view.frame;
frame.size = NSMakeSize(<#new width#>, <#new height#>);
view.frame = frame;

Try the - (NSRect)bounds method.

Related

Making an NSImage out of an NSView

I know how to create an NSImage depicting an NSView and all its subviews, but what I'm after is an NSImage of a view ignoring its subviews. I can think of ways of doing this with a subclass of NSView, but I'm keen to avoid subclassing if possible. Does anyone have any ideas?
Hide the subviews, grab the image, unhide the subviews:
NSMutableArray* hiddenViews = [[NSMutableArray] alloc init];
for (NSView* subview in [self subviews]) {
if (subview hidden) [hiddenViews addObject: subview];
else [subview setHidden:YES];
}
NSSize imgSize = self.bounds.size;
NSBitmapImageRep * bir = [self bitmapImageRepForCachingDisplayInRect:[self bounds]];
[bir setSize:imgSize];
[self cacheDisplayInRect:[self bounds] toBitmapImageRep:bir];
NSImage* image = [[NSImage alloc] initWithSize:imgSize];
[image addRepresentation:bir];
for (NSView* subview in [self subviews]) {
if (![hiddenViews containsObject: subview])
[subview setHidden:NO];
}
I would suggest making a copy of the desired NSView offscreen and taking a snapshot of that.

Unable to properly position NSImageView in NSView

I would like to display an NSImageView in my main View:
img=[[NSImageView alloc]init];
[img setImage:[[NSImage alloc]initWithContentsOfFile:filename]];
width=img.image.size.width/2;
height=img.image.size.height/2;
[img setFrame:NSMakeRect(0,0,width ,height)];
mainview = [((AppDelegate *)[NSApp delegate]).window contentView];
[mainview addSubview:img];
The image is properly displayed, however instead of being in the top-left corner of my main window, it's completely going to the right side of the screen.
What is wrong above?
Try like this:-
- (void)windowDidLoad
{
NSString *imageName = [[NSBundle mainBundle] pathForResource:#"yourImage ofType:#"tiff"];
NSImage *photoImage = [[NSImage alloc] initWithContentsOfFile:imageName];
[yourimgView setImage:photoImage];
CGFloat width=yourimgView.bounds.size.width;
CGFloat height=yourimgView.bounds.size.height;
CGFloat xboundspoint=yourimgView.bounds.origin.x;
CGFloat yboundspoint=yourimgView.bounds.origin.y;
[yourimgView setFrame:NSMakeRect(xboundspoint, yboundspoint+150.0, width, height)];
[yourview addSubview:yourimgView];
[[[self window]contentView]addSubview:yourview];
[super windowDidLoad];
}

Scrolling on NSTextView not working

I am appending some text on my NSTextView on the screen and so I want user to be able to scroll in the view to see the entire log. But I am not able to have my text view scrolling. Below is my code. Any idea whats wrong here.
NSView *superview = [window contentView];
NSRect scrollViewFrame = NSMakeRect(10, self.window.frame.size.height / 2 - 200, 400, 400);
NSScrollView *scrollview = [[NSScrollView alloc]
initWithFrame:scrollViewFrame];
NSSize contentSize = [scrollview contentSize];
[scrollview setBorderType:NSLineBorder];
[scrollview setHasVerticalScroller:YES];
[scrollview setHasHorizontalScroller:NO];
[scrollview setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
NSRect textViewFrame = NSMakeRect(0, 0, contentSize.width, contentSize.height);
self.logTextView = [[NSTextView alloc] initWithFrame:textViewFrame];
[self.logTextView setMinSize:NSMakeSize(0.0, contentSize.height)];
[self.logTextView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
[self.logTextView setVerticallyResizable:YES];
[self.logTextView setHorizontallyResizable:NO];
[self.logTextView setAutoresizingMask:NSViewWidthSizable];
[scrollview addSubview:self.logTextView];
[superview addSubview:scrollview];
This is fixed. I forgot to set the document view for my scrollview.
[scrollview setDocumentView:self.logTextView];

NSProgressIndicator in NSStatusItem

I have NSStatusItem with a custom NSView which shows images.
Whether the menu is opened or not it show different images just like that:
isMenuVisible = NO;
- (void)awakeFromNib {
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[statusItem retain];
dragStatusView = [[DragStatusView alloc] init];
[dragStatusView retain];
dragStatusView.statusItem = statusItem;
[dragStatusView setMenu:statusMenu];
[dragStatusView setToolTip:NSLocalizedString(#"Menubar Countdown",
#"Status Item Tooltip")];
[statusItem setView:dragStatusView];
[dragStatusView setTitle:#"11"];
}
- (void)drawImage:(NSImage *)aImage centeredInRect:(NSRect)aRect{
NSRect imageRect = NSMakeRect((CGFloat)round(aRect.size.width*0.5f-aImage.size.width*0.5f),
(CGFloat)round(aRect.size.height*0.5f-aImage.size.height*0.5f),
aImage.size.width,
aImage.size.height);
[aImage drawInRect:imageRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0f];
}
- (void)drawRect:(NSRect)rect {
// Draw status bar background, highlighted if menu is showing
[statusItem drawStatusBarBackgroundInRect:[self bounds]
withHighlight:isMenuVisible];
if(isMenuVisible) {
[self drawImage:image2 centeredInRect:rect];
}else {
[self drawImage:image1 centeredInRect:rect];
}}
(Of course this is not everything, but i hope all the relevant code to understand my problem)
Now i want to show a NSProgressIndicator in this NSView (in this NSStatusItem) if an upload is proceeding which means
1. Set the NSProgressIndicator when upload starts
2.Received something ? ==> Hide NSProgressIndicator show the image again.
How would i solve this ?
Please help me. Thanks
Here is the solution for View based status item.
NSStatusBar *bar = [NSStatusBar systemStatusBar];
self.theItem = [bar statusItemWithLength:NSVariableStatusItemLength];
self.statusView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 40, 20)];
NSProgressIndicator* progress = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(0, 0, 40, 20)];
[progress setIndeterminate:NO];
[self.statusView addSubview:progress];
self.theItem.view = self.statusView;
You can also draw a progress bar your self in Menu based status item
on progress change you can simply change the image of your status item.
- (void) updateIconWithProgress:(float)aProgress
{
NSImage* image = [NSImage imageNamed:#"small_icon_16_16.png"];
NSImage* img = [image copy];
[img lockFocus];
NSRect rect = NSMakeRect(2, 4, 12.0*aProgress, 8.0);
[[NSColor blueColor] set];
[NSBezierPath fillRect:rect];
[img unlockFocus];
[self.theItem setImage:img];
}
p.s. The code was written for ARC (Automatic Reference Counting) so there is no retain or release.

How to stretch (scale) image on a UIView?

How can i take an image and stretch it to fill the screen on my UIView (320/460)
I am loading and placing it, however as you can see, some scaling is needed.
Thank you!
CGRect gameArea = CGRectMake(0, 0, 320, 460);
UIImage *backgroundImage = [UIImage imageNamed:#"green_background.jpg"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:backgroundImage];
[myImageView setContentMode:UIViewContentModeScaleToFill];
UIView *topView = [[UIView alloc] initWithFrame:gameArea];
[topView addSubview:myImageView];
[[self view] addSubview:topView];
Like I said in my comments above to Shamsiddin's post, the UIImageView needs a size to know where to stretch to. Here I'm using the same size of the UIView. I've added another answer because the code to UIView is missing. So here's the complete code.
// Area of the UIView and UIImageView
CGRect gameArea = CGRectMake(0, 0, 320, 460);
// Create UIImageView with Image
UIImage *backgroundImage = [UIImage imageNamed:#"green_background.jpg"];
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:gameArea];
[myImageView setImage:[backgroundImage stretchableImageWithLeftCapWidth:3 topCapHeight:3]];
[myImageView setUserInteractionEnabled:YES];
// Create UIView and insert subview
UIView *topView = [[UIView alloc] initWithFrame:gameArea];
[topView addSubview:myImageView];
[[self view] addSubview:topView];
CGRect gameArea = CGRectMake(0, 0, 320, 460);
UIImage *backgroundImage = [UIImage imageNamed:#"green_background.jpg"];
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:gameArea];
[myImageView setImage:[backgroundImage stretchableImageWithLeftCapWidth:3 topCapHeight:3]];
[myImageView setUserInteractionEnabled:YES];
[self.view addSubview:myImageView];
try
UIView *view;
view.ContentMode = UIViewContentModeScaleToFill;
I just did this in my own iOS5 app, i needed to dynamically set the background image and scale it. Imagine you are doing this in - (void)viewWillAppear:(BOOL) animated
UIImageView *imageview = [[UIImageView alloc] initWithFrame:self.view.frame];
[imageview setContentMode:UIViewContentModeScaleAspectFill];
[imageview setImage:[UIImage imageNamed:#"Background.png"]];
[self.view addSubview:imageview];
[self.view sendSubviewToBack:imageview];
So we create the view, set it's scaling mode, add the bg image, set it as the subview in the main view and then push it to the back so it does not occupy any other subviews.
Please accept this answer.

Resources