使用cocos2d做界面的时候,在计算坐标数据的时候都是浮点型,而在屏幕上绘图时是以像素为单位的,这里必然存在数据精度的丢失。一般情况下这种误差是可以容忍的,但是当需要平铺一组连续的图片时就可能存在问题,出现图片不连续的情况。这时只能自己绘图了。
将一组图片在水平方向连续排列,并获得新图形:
//根据image数组获得一个新的image,将这些image横向排列
+(UIImage *) getSumImageWithImagesHorizontal:(NSArray*) images {
CGFloat width = 0;
CGFloat height = 0;
for (int i = 0; i < [images count]; i++) {
CGSize size = [(UIImage*)[images objectAtIndex:i] size];
width += size.width;
height = size.height > height ? size.height : height;
}
UIGraphicsBeginImageContext(CGSizeMake(width, height));
CGPoint beginPoint = ccp(0, 0);
for (int i = 0; i < [images count]; i++) {
UIImage* image = (UIImage*)[images objectAtIndex:i];
[image drawInRect:CGRectMake(beginPoint.x, beginPoint.y, image.size.width, image.size.height)];
beginPoint.x = beginPoint.x + image.size.width;
}
UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImg;
}