Cocoa: how to connect a button to a view? - cocoa

The title question might be a little vague. I will try to explain as good as i can (also note im new to cocoa programming so excuse any mistakes :) ).
I have a VIEW that extends NSView where i would like to draw some images.
I want the images to be loaded from some files on the disk, when i click a button.
I managed to do all the loading into an NSMutableArray. My problem is, how to setup things:
- i need the NSMutableArray with the images (paths) to be available for the loading function (so i put it in a controller and assigned an action to the button in the UI). but in this case, how can i acces it in the drawRect function of my custom view class?
if i put it as a member of my custom view class, i dont know how to link it to the button , so that on the button click , the array will be filled with images paths.
I guess im a bit confused since im new to osx programming , on how these things get connected.
Hope someone could clear the air a little for me . Thanks.

The images view should have a property to hold an array of images, and the controller should set this property to its array of images. (The images view should, of course, make its own shallow copy of the array.)
images (paths)
Assuming you mean pathnames, images are not paths, and paths are not images. The controller should load the images and pass the images off to the view. The view should not care where the images might have come from.

Related

Image gallery in CollectionViewController

I am recently working on an iphone app in xcode 6 and created an image gallery in CollectionViewControllor, I need help about 2 things;
first I need to get a fullscreen view of the Images I have in each cell,
second How I can update the images dynamically
I really appreciate your help in advance,
A few suggestions on the two questions in this post
How to show image in full screen? Depends how you want it to show to user. One way could be to make a new view controller having a UIImage. Once you tap on the image in collection view (using didSelectItemAtIndexPath), you can pass the image to new controller which displays it.
To update the images in real time, I assume you have images stored somewhere remotely and want to fetch those in background and update the collection view. Following article may help over here
http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues
Hope it helps.

How to determine size of page in uipageviewcontroller landscape?

I am working with the default UIPageViewController template from Apple. Basically just dabbling with it to learn how it works.
I would like to load a background image for the dataview controller and resize automatically it according to the orientation (rather than loading different images).
Is there any quick way I'm missing to determine the size of each page in landscape orientation? I don't mean
self.pageViewController.view.frame
which seems to give the size of the entire view including both pages, but rather the size of one of the two pages shown in landscape orientation. I've achieved more or less what I need with something like this
CGRectOffset(CGRectInset(self.pageViewController.view.bounds,125,0), -125, 0)];
but surely there must be some better way? This feels like an ugly hack.
Thanks in advance!
I havent worked with the split view UIPageViewController yet, but as I understand it, you'd pass in two views which would be rendered as the two pages. So why not let the page views determine the size of the graphic itself based on how the UIPageViewController has set it in the frame?

how to translate and scale a NSImage?

I have built so far an application that allows the user to drag and drop images onto a NSImageView. However, I want to be able to move these images by simply clicking on any image and hold down the mouse button to move it's location.
How can I manipulate NSImageView to translate/scale after setting the images down? Is that possible? I've read about the NSAffineTransform, but it seems like that is moving the images before creating the image itself. I already have the images on the canvas, and simply want to click and hold the image and move it with my mouse. Please help anyone!
There are two sides to this.
NSImage is the model object, which you might want to display in different ways, save to disk/archive, etc. If you want to actually change the model (scaling, rotating, etc.), implying a permanent change, then you are going to probably want to look at NSAffineTransform, Quartz drawing, etc.
But you probably didn't mean that. Instead you probably are interested in NSImageView, which is a view object, displaying the contents of the NSImage model object using whatever display attributes are desired. If you only want to change how an image is displayed, not what the actual bytes in the image are, then you are going to manipulate the NSImageView at run-time. You can use NSAffineTransform here as well, but it's somewhat uncommon (and usually unnecessary).
The key thing to note that is the NSImageView inherits from NSView, so you have all its power at your disposal. Take a look at certain methods, such as:
-setFrameSize: - useful for changing the view size, and thus the image display scale
-setFrameOrigin: - useful for changing the view position, and thus the apparent image position
Note again that these have nothing to do with images per se, and apply to all Cocoa views. You may want to take a look at a book like Cocoa Programming for Mac OS X to get you past the basics. (You can then do more interesting things, like rotation, animation, etc.)

Touch to move through images

I have an NSMutableArray of jpg images and I want to be able to touch and, moving my finger from right to left, move through the array of images in order. I've been reading about UIScrollView, but I'm not sure if this accomplishes what I am looking for. It seems to me that UIScrollView is more used for viewing an object that is too large to be displayed entirely on the screen and you need to be able to move to see every part of the object. I need to move to an entirely new image, the next image in my array. What is the best way to go about this?
You certainly can use a UIScrollView for what you want. A scroll view can be used, as you noted, to scroll an area that is too large for the screen. However, you can also use it to scroll through "pages". This is really the best way to do what you want, short of rolling your won scroll view.
There is a property of UIScrollView, called pagingEnabled, which you'll want to set to YES. I suggest that you give it a try. Also, have a look at the UIScrollView documentation and the Scroll View programming guide. It will tell you all that you need to know about UIScrollViews. Also, remember that for proper memory management, you'll want to only load three images at any given time. See this answer that I wrote for more information on that, and on UIScrollViews in general.

How to check the positions of different controls in iPhone

How to check some place is already reserved by some controls before adding a new control to the view.
I have added some buttons in the UIImageView and I want to display some images in a label which I'm getting in a random size from the server.I want to place those images over the empty places in the same view.
After adding the first image in some empty place,how could I know that this place is reserved and I have to place the second image in some other placeand the images and buttons should not overlap over each other.
Can somebody please help me ?
Try using CGRectIntersectsRect on the control's frame property (which it inherits from UIView).
http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CGGeometry/Reference/reference.html
I don't believe this will work for rotated views though...hm.
See this image for reference: http://cl.ly/3T3d3w0D2I2Q1w1r0m46

Resources