關於圖片查看的小結

在做圖片瀏覽的時候,總要涉及到把圖片整個顯示到屏幕上,需要做如下判斷:
這裏寫圖片描述
代碼如下:
- (void)calFrame{

CGSize size = self.image.size;

CGFloat w = size.width;
CGFloat h = size.height;

CGRect superFrame = self.screenBounds;
CGFloat superW =superFrame.size.width ;
CGFloat superH =superFrame.size.height;

CGFloat calW = superW;
CGFloat calH = superW;

if (w>=h) {//較寬

    if(w> superW){//比屏幕寬

        CGFloat scale = superW / w;

        //確定寬度
        calW = w * scale;
        calH = h * scale;

    }else if(w <= superW){//比屏幕窄,直接居中顯示

        calW = w;
        calH = h;
    }

}else if(w<h){//較高

    CGFloat scale1 = superH / h;
    CGFloat scale2 = superW / w;

    BOOL isFat = w * scale1 > superW;//比較胖

    CGFloat scale =isFat ? scale2 : scale1;

    if(h> superH){//比屏幕高

        //確定寬度
        calW = w * scale;
        calH = h * scale;
    }else if(h <= superH){//比屏幕窄,直接居中顯示

        if(w>superW){

            //確定寬度
            calW = w * scale;
            calH = h * scale;
        }else{
            calW = w;
            calH = h;
        }
    }
}

CGRect frame = [UIView frameWithW:calW h:calH center:self.screenCenter];

self.calF = frame;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章