iphone 图片等比例缩小

图片缩放与适应不同屏幕比例算法
本文探讨了一种高效算法,用于根据图片原始宽高比和目标屏幕宽高比,自动调整图片尺寸,使其既不失真又适应不同设备屏幕尺寸。通过计算纵横比并依据目标屏幕尺寸进行缩放,确保图片在各种设备上都能呈现最佳视觉效果。
- (CGSize)setoriginW: (CGSize)imageWH setoriginH:(CGSize)originWH

{ 
    float dWidth = imageWH.width;                                  //img的宽高
    float dHeight = imageWH.height;
    float dAspectRatio = dWidth/dHeight;                           //纵横比
    
    float dPictureWidth = originWH.width;
    float dPictureHeight = originWH.height;                        //传图的宽高
    float dPictureAspectRatio = dPictureWidth/dPictureHeight;      //长宽比
    
    
    if (dPictureAspectRatio > dAspectRatio){
        
        float nNewHeight = dWidth/dPictureWidth*dPictureHeight;  
        newImage = CGSizeMake(dWidth, nNewHeight);
        
        
    }else if (dPictureAspectRatio < dAspectRatio){
        
        float nNewWidth = dHeight/dPictureHeight*dPictureWidth;   
        newImage = CGSizeMake(nNewWidth, dHeight);
        NSLog(@"newImage = %f \n %f",newImage.width,newImage.height);
    }

    return  newImage;}


///其它方法,,,不太合适各种图片

//    sizeWidth = originWH.width;  //图的宽
//    sizeHeight = originWH.height; //图的高
//    
//    float imgwidth = imageWH.width;    //视图的宽
//    float imgHeght = imageWH.height;   //视图的高
//    float w = 0;
//    float h = 0;

//    
//    if (sizeWidth >= imgwidth  && sizeHeight <= imgHeght) {
//        
//        newWidth = imgwidth;
//        newHeight = (imgHeght * sizeHeight) / sizeWidth;
//    }
//    
//    else if(imgwidth <= sizeWidth && imgHeght >= sizeHeight){
//        
//        newHeight = imgHeght;
//        newWidth = imgHeght * sizeWidth / sizeHeight;
//        
//    }
//    
//    else if (sizeWidth >= imgwidth && sizeHeight >= imgHeght){
//        
//        if(sizeWidth / sizeHeight >= imgwidth / imgHeght)
//        {
//            newWidth = imgwidth;
//            newHeight = (imgHeght * sizeHeight) / sizeWidth; 
//        }
//        
//        if(sizeWidth / sizeHeight <= imgwidth / imgHeght)
//        {
//            newHeight = imgHeght;
//            newWidth = imgHeght * sizeWidth / sizeHeight;
//        }
//    }
//    
//    else
//    {
//        newWidth = sizeWidth;
//        newHeight = sizeWidth;
//    }
    --------------------------------------------
//    if(sizeWidth > 0 && sizeHeight > 0){    
//        if(imgwidth / sizeHeight >= imgwidth / imgHeght){    
//            if(sizeWidth > imgwidth){      
//                newImage.width = imgwidth;    
//                newImage.height = (sizeHeight * imgwidth) / sizeWidth;    
//            }else{    
//                newImage.width = sizeWidth;      
//                newImage.height = sizeHeight;    
//            }    
//        }else{    
//            if(sizeHeight > imgwidth){      
//                newImage.height = imgHeght;    
//                newImage.width=(sizeWidth * imgHeght) / sizeHeight;            
//            }else{    
//                newImage.width = sizeWidth;      
//                newImage.height = sizeHeight;    
//            }    
//        }    
//    }    
    
------------------------------
    
    // 原始宽高在指定宽高范围内,不作任何处理
//    if (sizeWidth <= imgwidth && sizeHeight <= imgHeght) {
//        newImage.width = sizeWidth;
//        newImage.height = sizeHeight;
//    }else{
//        //取得比例系数
//        w = sizeWidth / imgwidth;
//        h = sizeHeight / imgHeght;
//        NSLog(@"\nwwwwwwwwww=====%f\nhhhhhhhhhhhh====%f",w,h);
//    }
//    if (w > h) {
//        newImage.width = imgwidth;
//        newImage.height = (float)(w >= 1 ? roundf(imgHeght / w)  : roundf(imgHeght * w));
//    }
//    // 宽度比小于高度比
//    else if (w < h )
//    {
//        newImage.height = imgHeght;
//        newImage.width = (float)(h >= 1 ? roundf(imgwidth / h) : roundf(imgwidth * h));
//    }
//    // 宽度比等于高度比  
//    else  
//    {  
//        newImage.width = imgwidth;  
//        newImage.height = imgwidth;  
//    }  
//







                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值