Creating images from views, strings, etc - macos

Ok so I have a problem. I have a method that is called imageFromText, it requires one parameter, the string itself, and it returns a NSImage. I also have another one which is called: imageFromView, this one basically have to "take a screenshot" of the view and return a NSImage, it also has only one parameter, the view itself. So it looks like this:
-(NSImage*)imageFromText: (NSString*)text {
}
-(NSImage*)imageFromView: (NSView*)view {
}
There's only one problem, I have no idea how to do this. Well so, I spent my afternoon searching around and I didn't find nothing. I've tried, to the second one, a method dataWithPDFInsideRect, but obviously, this method was not made to this propose. Please help me out!.
PLEASE NOT: I'M NOT ASKING FOR THE CODE READY. LIKE THE OLD DICTATION (IN MY COUNTY): DON'T GIVE THEM THE FISH, THEACH THEM HOW TO FISH. (TRANSLATED).

An alternative way is to lock focus on the view, then create a bitmap image rep with the contents of the view's bounds. You can then create a blank image whose size is the size of the bounds, and add the image rep to it.
The third way is dataWithPDFInsideRect:. Yes, the one you tried and couldn't get to work (I wish you'd explained what problem you had with it instead of just dismissing it!). Pass the view's bounds, then pass the data to NSImage's initWithData:.

As for imageFromView: check the Organizer Documentation for Screen Capture.
And imageFromText: You want an image (PNG I assume) that just shows a text? Don't you want to specify things like image size, font size, font color, background color, ...?

Summarizing, lock focus on the image, then draw. The NSImage docs should have more if you search for lockFocus.

The methods you're interested in are:
-[NSImage lockFocus]
+[NSGraphicsContext currentContext]
-[NSView displayRectIgnoringOpacity:inContext:]
-[NSImage lockFocus]
To draw to an image, allocate one, and then lock focus on it, then issue drawing calls and then unlock focus.
To draw a view into an image, lock focus on an image, get the current graphic context (which is now the image), and pass that to -[NSView displayRectIgnoringOpacity:inContext:].

Related

How to save drawings in Livecode?

I'm a bit of a Livecode newbie so I apologize if this is obvious. But I created a stack. Added fields, buttons, code etc. Then I drew and painted some images. Saved. When I reopened the stack, all my image work was gone! aqrrg. Happened twice. Why?
Make sure you add an image object before drawing. Adjust it to the size you need, then draw on the image object and it will be saved with your stack. If you fail to add an image object first, your drawing will disappear, not sure if this is a bug or not, but it isn't logical because you do see the drawing.

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.)

How can I add a UILabel so its text is 2 pixels below the "text" of another UILabel?

I've really searched for an answer to this one but not come up with anything solid so here goes.
I'm dynamically adding UILabels to a view, they could be any size.
I would like the second added one to be placed about 2 pixels below the last regardless whether there is letters that go bellow the line.
I've tried using various method the get the labels frame after it has been sized to font but with no look. The frame always seems to be bigger than the actual text vertically.
Have you guys got any ideas?
First, I'd try setting the label's adjustsFontSizeToFitWidth to NO and see if that it will allow you to mess up the normal proportions, as seems to be your goal.
If that doesn't work, you probably need to draw the string to a CALayer. Create a CALayer and add it to your view's root layer, i.e.:
[yourView.layer addSublayer.yourNewLayerForString].
Set the sublayer's delegate to the class which will do the drawing:
[yourNewLayerForString setDelegate:self];
And implement drawLayer in that class (the one you assigned as delegate). The drawLayer code can be kind of ugly, but you can find examples out there for how to draw a string in a layer. This one looks good: addTextToLayer.
You will then have exact control over the font and string size, and can adjust the layer frame and the view frame to suit your aims.

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.

Qt - Drawing a Rect/Frame out of a bigger Pixmap image

I've been banging my head about this seemingly easy task and I could really use some help.
I have a wide Image loaded in the gui (using the designer..) and I want to be able to draw only a portion of it, a rectangle.
I need to be able to change this rectangle position over the large image, in order to draw a different part of the larger image at will. In this process the rect must maintain its size.
Using the Ui::MainWindow object I'm able to access the label holding the image and a solution that involves using this option is preferred (in order to keep up with the rest of the code I've already written )
Any solution will be much appreciated :)
Thanks,
Itamar
I would definitely (for ease of use) just place an empty label as placeholder in Designer.
Then implement the paintEvent for this label (delegate it to your own method). You'll have also have to look into QPainter, QPixMap, etc... Should be doable based on these hints and the documentation.
If you want more, I suggest you provide a small code snippet to work upon.
If you want to do this more or less purely through designer, you could put a QScrollArea where you want the portion of the image to appear. If you set the scroll area's scrollbar policy to be never shown, you can then manually change what part is visible via the scroll area widget. However, this would probably be more complex that creating a derived widget and reimplementing the paint function.

Resources