需要使用CCSprite
以多种尺寸显示图片。通常添加到CCSprite
的图片都有固定的宽高。有没有方法能让我像下面代码一样,不用一开始手动设置图片的尺寸。然后将图片放到ccpsrite。
-(UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize image:(UIImage*)sourceImage
{
UIImage *newImage = sourceImage;
CGSize imageSize = sourceImage.size;
/// Source image is of desired size or desired size 0x0, no change is done
if (!CGSizeEqualToSize(targetSize, CGSizeZero) && !CGSizeEqualToSize(imageSize, targetSize))
{
CGFloat aspectRatio = imageSize.width / imageSize.height;
CGFloat newAspectRatio = targetSize.width / targetSize.height;
CGSize tempSize = targetSize;
if (newAspectRatio < aspectRatio)
{
tempSize.width = targetSize.width * aspectRatio / newAspectRatio;
}
else
{
tempSize.height = targetSize.height * newAspectRatio / aspectRatio;
}
UIGraphicsBeginImageContext(targetSize);
[sourceImage drawInRect:CGRectMake((targetSize.width - tempSize.width) / 2.0, (targetSize.height - tempSize.height) / 2.0, tempSize.width, tempSize.height)];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return newImage;
}
可以调整CCSprite. CCSprite
是CCNode
的子类,CCNode
有这些属性:
/** The scale factor of the node. 1.0 is the default scale factor. It modifies the X and Y scale at the same time. */
@property(nonatomic,readwrite,assign) float scale;
/** The scale factor of the node. 1.0 is the default scale factor. It only modifies the X scale factor. */
@property(nonatomic,readwrite,assign) float scaleX;
/** The scale factor of the node. 1.0 is the default scale factor. It only modifies the Y scale factor. */
@property(nonatomic,readwrite,assign) float scaleY;
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。