UIImage的位置

时间:2011-07-20 14:39:51

标签: xcode uiimage

我正在使用以下代码在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();

获得此结果:

http://cl.ly/8bSP

但我想得到这个:

http://cl.ly/8c6Q

我无法弄清楚,我很困惑。

有人可以帮助我吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

如果图像的最后一行少于三个,则需要为图像添加X偏移量:

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

    int imagesLeft = min(3, [theArray count] - i);
    int offs = (3 - imagesLeft) * 64 / 2;

    for (int k = 0; k < imagesLeft; k++) {
        image=[UIImage imageNamed:[NSString stringWithFormat:@"%@",[theArray objectAtIndex:i]]];
        [image drawAtPoint:CGPointMake(k*64 + offs, posY*23)];
    }
    posY++;
}