现在的位置: 首页 > 综合 > 正文

lua管理图片资源-很简单的代码

2013年08月21日 ⁄ 综合 ⁄ 共 479字 ⁄ 字号 评论关闭
单实例管理图片加载防止重复加载使用导致内存问题

imgDef = {}
function getImageByName(name)
    if (imgDef[name] == nil) then
        imgDef[name] = UIImage:imageNamed(name)
    end
    return imgDef[name];
end

如果涉及图片是retina的 需要吧retina图片分割成不同部分

function splitePNG(img, rect)
    local scale = img:scale()
    rect.x = rect.x*scale
    rect.y = rect.y*scale
    rect.width = rect.width*scale
    rect.height = rect.height*scale
    local cgimage = Game.CGImageCreateWithImageInRect(img, rect)
    local image   = UIImage:imageWithCGImage_scale_orientation(cgimage, scale, 0)
    puts(img:scale())
    return image
end

抱歉!评论已关闭.