您的位置:首页 > 移动开发 > IOS开发

IOS开发 如果图片太大的缩小算法,通用多种情况

2015-03-03 11:33 302 查看
- (CGSize)getSizeWithHeight:(CGFloat)height width:(CGFloat)width
{
    CGSize size = {0,0};
    CGFloat fHeight = [UIScreen
mainScreen].bounds.size.height;
    CGFloat fWidth = [UIScreen
mainScreen].bounds.size.width;
    if (height < fHeight && width < fWidth)
    {
        size.height = height;
        size.width = width;
    }
    else
    {
        if (height >= fHeight && width <= fWidth)
        {
            CGFloat  mult = fHeight/height;
            size.height = fHeight;
            size.width = width*mult;
        }
        else if (height <= fHeight && width >= fWidth)
        {
            CGFloat  mult = fWidth/width;
            size.height = height*mult;
            size.width = fWidth;
        }
        else if (height >= fHeight && width >= fWidth)
        {
            CGFloat  multW = fWidth/width;
            CGFloat  multH = fHeight/height;
            if (multW <= multH)
            {
                size.height = height*multW;
                size.width = fWidth;
            }
            else
            {
                size.width = width*multH;
                size.height = fHeight;
            }
        }
    }
    
    return size;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 应用 算法