开发者

UIImage position [duplicate]

This question already has an answer here: Closed 11 years ago.

Possible Duplicate:

UIImage position

I'm using the following code to place some images in the UIView:

UIImage *image;
UIGraphicsBeginImageContext(CGSizeMake(480, 320));
int k=0;
int posY=0;

for (int i=0; i<[theArray count]; i++) {

    image=[UIImage imageNamed:[NSString stringWithFormat:@"%@",[theArray objectAtIndex:i]]];
    if (k>2) {
        k=0;
        posY++;
    }       

    [image drawAtPoint:CGPointMake(k*64, posY*23)];

    k++;

}


UIImage *combinatedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

obtaining this result

http://cl.ly/8bSP

but i would like to obtain this:

http://cl.ly/8c6Q

i can't figure out, i am so confused. Can somebody help me, please??? thanks!!

[SOLVED]

This is my final working solution

UIGraphicsBeginImageContext(CGSizeMake(480, 320));

    int k=0;

    int posY=0;

    int d = 0;

    for (int i=0; i<[theArray count]; i++) {
        int imagesLeft = MIN(3, [theArray count]-i);
        image=[UIImage imageNamed:[NSString stringWithFormat:@"%@",[theArray objectAtIndex:i]]];

        if (k>2) {
            k=0; 

      开发者_运维问答      d=(3-imagesLeft)*32; 
            posY++; 
        }

        [image drawAtPoint:CGPointMake(k*64+d, posY*23)]; 

        k++; 
        NSLog(@"image nr. %i = %i",i,imagesLeft);
    }
    UIImage *combinatedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext();

Thanks for suggestions!


You're drawing the image at a position defined by

[image drawAtPoint:CGPointMake(k*64, posY*23)];

When you go to the second row you reset k to zero. So it's going to draw left-aligned, just like the first row. You need to add a "delta":

int k=0;
int posY=0;
int delta = 0;

for (int i=0; i<[theArray count]; i++) {

image=[UIImage imageNamed:[NSString stringWithFormat:@"%@",[theArray objectAtIndex:i]]];
if (k>2) {
    k=0;
    posY++;
    delta = 30;
}       

[image drawAtPoint:CGPointMake(k*64 + delta, posY*23)];

k++;
}

Of course, this only works for five elements. If you don't know how many elements you'll have in a row, you need to precompute that so that you only add the appropriate delta to the last row.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜