how to distribute all image with poper tag
i try to create multiple image array and now i want to add proper tag with specific deck like(Hearts,Diamonds,Spades,Clubs). so in image name as C=dimond, R=Heart,K=spades,F=Clubs and image no as 11=Jack,12=Queen,13=King...so how to distribute it with proper tag so i can indentify specific image.
->>>>>>>>
imagearr =
[[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"C1.png"],
[UIImage imageNamed:@"C2.png"],
[UIImage imageNamed:@"C3.png"],
[UIImage imageNamed:@"C4.png"],
[UIImage imageNamed:@"C5.png"],
[UIImage imageNamed:@"C6.png"],
[UIImage imageNamed:@"C7.png"],
[UIImage imageNamed:@"C8.png"],
[UIImage imageNamed:@"C9.png"],
[UIImage imageNamed:@"C10.png"],
[UIImage imageNamed:@"C11.png"],
[UIImage imageNamed:@"C12.png"],
[UIImage imageNamed:@"C13.png"],
[UIImage imageNamed:@"F1.png"],
[UIImage imageNamed:@"F2.png"],
开发者_开发百科 [UIImage imageNamed:@"F3.png"],
[UIImage imageNamed:@"F4.png"],
[UIImage imageNamed:@"F5.png"],
[UIImage imageNamed:@"F6.png"],
[UIImage imageNamed:@"F7.png"],
[UIImage imageNamed:@"F8.png"],
[UIImage imageNamed:@"F9.png"],
[UIImage imageNamed:@"F10.png"],
[UIImage imageNamed:@"F11.png"],
[UIImage imageNamed:@"F12.png"],
[UIImage imageNamed:@"F13.png"],
[UIImage imageNamed:@"K1.png"],
[UIImage imageNamed:@"K2.png"],
[UIImage imageNamed:@"K3.png"],
[UIImage imageNamed:@"K4.png"],
[UIImage imageNamed:@"K5.png"],
[UIImage imageNamed:@"K6.png"],
[UIImage imageNamed:@"K7.png"],
[UIImage imageNamed:@"K8.png"],
[UIImage imageNamed:@"K9.png"],
[UIImage imageNamed:@"K10.png"],
[UIImage imageNamed:@"K11.png"],
[UIImage imageNamed:@"k12.png"],
[UIImage imageNamed:@"K13.png"],
[UIImage imageNamed:@"R1.png"],
[UIImage imageNamed:@"R2.png"],
[UIImage imageNamed:@"R3.png"],
[UIImage imageNamed:@"R4.png"],
[UIImage imageNamed:@"R5.png"],
[UIImage imageNamed:@"R6.png"],
[UIImage imageNamed:@"R7.png"],
[UIImage imageNamed:@"R8.png"],
[UIImage imageNamed:@"R9.png"],
[UIImage imageNamed:@"R10.png"],
[UIImage imageNamed:@"R11.png"],
[UIImage imageNamed:@"R12.png"],
[UIImage imageNamed:@"R13.png"],nil];
..............
There's a better work around this issue rather than taking a bunch of images in the array. Initialize an Enum with all different deck categories and loading the image through dynamically generated name for e.g:
// In a header file
typedef enum {
HEARTS, // Hearts
DIAMONDS, // Diamonds
SPADES, // Spades
CLUBS // Club
} DeckType;
// In a source file
NSString * const DeckType_toString[] = {
@"H",
@"D",
@"S",
@"C"
};
- (UIImage *)getDeckImage:(DeckType)deckType withCardNo:(NSInteger)cardNo {
return [UIImage imageNamed:[NSString stringWithFormat:@"%@%d.png", DeckType_toString[deckType], cardNo]];
}
You can also take another enum with group of 1...13 cards and used them instead of cardNo
I am not sure,you could create a UIImage
with tag
.because it's inherited from NSObject
NOT UIView
which provide you an integer tag variable and used by the UIView
class.
But still you could do it by writing a wrapper for you UIView
class, and should be like below
use below as reference.
@interface MyUIImageWrapper: NSObject
{
UIImage* iMyImage;
int tag;
}
you could have some init function which take two argument one for image name and others for tag value
Like below
-(id) initWithTag:(NSString*) fileName withTag:(int) myTag ;
And the implementation for the method.
-(id) initWithTag:(NSString*) fileName withTag:(int) myTag
{
if(self = [super init])
{
iMyImage = [UIImage imageNamed:fileName];
tag = myTag;
}
return self;
}
To identify images in a collection I'm suggesting an NSDictionary instead of an NSArray so every image is associated with an NSString, e.g. [[NSDictionary alloc] initWithObjectsAndKeys:..., [UIImage imageNamed:@"R7.png"], @"R7", ...]; tagging works for UIImageViews (all UIView descendants) and is more proper for retrieving already added subviews...
精彩评论