1
我在UIView上添加了一個Imageview,並設置了屬性AspectToFit。根據應用程序的要求,我需要在imageview上添加一個圖像。imageview和圖像縱橫比的區別
float width;
float height;
float x;
float y;
CGSize imageViewSize = self.getImageView.bounds.size;// self.getImageView.frame.size;
CGSize imageSize = self.getImage.size;
CGFloat minFactor = imageViewSize.width/imageSize.width;
if (imageViewSize.height/imageSize.height < minFactor)
{
minFactor = imageViewSize.height/imageSize.height;
}
CGRect frame = self.getImageView.frame;
NSLog(@"view height..%@",NSStringFromCGRect (frame));
CGSize resultSize = CGSizeMake(minFactor * imageSize.width, minFactor * imageSize.height);
UIImage *newimage = [self imageWithImage:self.getImage scaledToSize:resultSize];
float mainwidth = imageViewSize.width;
float mainheight = imageViewSize.height;
float imagewidth = newimage.size.width;
float imageheight = newimage.size.height;
_widthdiff = mainwidth-imagewidth;
_heightdiff = mainheight-imageheight;
x = self.view.frame.size.width-width-0-_widthdiff/2;
y = self.view.frame.size.height-height-116-_heightdiff/2;
imageview = [[UIImageView alloc]
initWithFrame:CGRectMake(x, y, width, height)];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
該代碼可用於除iPhone(iPhone 6另外,IPONE 7加)的所有設備工作正常。正如我所看到的,只有在Plus設備的大小上纔有問題。
有沒有什麼不同來計算imageview和正確的圖像之間的差異或比例? 請指教。
目前尚不清楚您面臨的具體問題。 – Shubhank
@Shubhank我編輯了我的問題,附上兩張圖片,一張是iPhone 5s,另一張是iPhone 6s Plus。你可以看到,qrcode的圖像位置是精確的,因爲每個uiimage開始在uiimageview方面適合後,但在iPhone 6s Plus中,qrcode的位置不正確。 –
分配了「寬度」和「高度」的值? – Shubhank