2015-09-28 137 views
0

我遇到了一個無法解決2天的問題。在將服務器映像插入到服務器映像中之前,我將它們在客戶端上剪切以適應屏幕。一切都很好,但圖像隨機高度變化。現在高度是獨立計算的 - 與寬度成正比。 普通影像:tableviewcell裏面的圖片大小調整

enter image description here

壞形象

enter image description here

因爲我動態我靠所有的細胞不同的設備我不能明確要求的UIImageView。

我調整大小功能:

-(UIImage *)resizeImage :(UIImage *)theImage :(CGSize)theNewSize { 
UIGraphicsBeginImageContextWithOptions(theNewSize, NO, 1.0); 
CGFloat height = theImage.size.height; 
CGFloat newHeight = 0; 
newHeight = (theNewSize.width * height)/theImage.size.width; 
newHeight = floorf(newHeight); 
NSLog(@"new height image %f", newHeight); 
[theImage drawInRect:CGRectMake(0, 0, theNewSize.width, newHeight)]; 
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
return newImage; 
} 

內layoutSubviews:

if(device == thisDeviceClass_iPhone5) { 
    [self.imageView setFrame:CGRectMake(0,0, 320, 255)]; //180 
    offset = 0; 
    padding = 5; 
} else if(device == thisDeviceClass_iPhone6){ 
    [self.imageView setFrame:CGRectMake(0,0, 375, 255)]; //211 
    offset = 25; 
} else if(device == thisDeviceClass_iPhone6plus) { 
    [self.imageView setFrame:CGRectMake(0,0, 414, 255)]; //233 
    offset = 40; 
} 

回答

1

你的做法是很老的學校。你對圖像的大小進行了「硬編碼」,這意味着如果明年蘋果想出了一款新的iPhone,但又有不同的尺寸,你會遇到麻煩。您應該考慮使用Apple推薦的自動佈局約束(這裏是一個很好的教程:http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1)。

您還可以將ImageView.contentMode設置爲UIViewContentModeScaleAspectFill,它將爲您裁剪並調整大小。

+0

是的,我知道這是不好的代碼,這是一個臨時解決方案。就像我沒有指定圖像的確切尺寸一樣,它會獨立移動,並在自定義單元類中傳遞屏幕的尺寸,我不知道如何。沒有變量self.view。 ... – user2759544

+0

我安裝了這個contentMode,但不能幫助圖像扭曲,並理解爲什麼我不能拉緊。 – user2759544

+0

您是否將imageView的「剪輯子視圖」設置爲YES?包括 – null