PDA

View Full Version : Select a Rectangle From a Source Image




DataBits
Aug 30, 2008, 07:06 AM
(2-D Poker Game based on UIKit)

I have a PNG resource image file containing a matrix of 52 playing cards and have a few questions on how to reference each card to be set into a view.

With DirectDraw, it is standard practice to load a large primary source image that is comprised of a matrix of smaller images within a single PNG resource into an offscreen surface. The developer is then allowed to define a source rectangle within the offscreen surface to BitBlt a specific image to the display at any location.

How do I do this with the UIKit's frameworks for a non-InterfaceBuilder based 2-D application?

If I load a large source image that is comprised of a matrix of smaller images with UIImageView or UIImage, how to I retrieve an individual image within the main source image an set it into the view?

It appears that we are only allowed to set the image position (x,y) within the view, along with width and height rather than define a rectangle within the source image when using the UIImageView, etc..

Do I really have to create 52 views to have access to each playing card when utilizing the UI-interface?

Is it typical practice to pre-load all of the image resources (PNG's) into views at the start of the application and release them when the application quits?

Basically, I would like to create 5 views (one for each card position) and then load a card deck image that dynamically set cards within the card deck image into each view. Is this possible with the UIImage or UIImageView?


Thanks guys.



Sbrocket
Aug 30, 2008, 03:21 PM
The best way I see of doing this without diverging from the method you described is to create 5 views using the single matrix image, then adjust the bounds of the UIImageView so that it contains only a single card. You could then simply readjust the bounds to change the displayed card.

Take a look at this if you aren't familiar with the distinction between a view's frame and bounds:
http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaViewsGuide/Coordinates/chapter_3_section_3.html

Note: That link uses the Cocoa coordinate system which has (0,0) at bottom-left instead of CocoaTouch's (0,0) at top-left.

DataBits
Aug 30, 2008, 06:57 PM
Thanks much, I had read the document before but missed the relationship between bounds rectangle and the view.

Setting the bounds is working well with the UIImage class.

I have also been looking into other methods of accomplishing the same results by using CGImage class library.

CGImageCreateWithImageInRect caught my eye.