2014-05-10 17 views
0

我從數據試圖加載圖像:的UIImageView忽略內容模式形式圖像數據

NSError *error ; 
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]; 
NSData *encryptedData = [NSData dataWithContentsOfFile:imagePath]; 
NSData *decryptedData = [RNDecryptor decryptData:encryptedData 
            withPassword:PASSWORD 
              error:&error]; 
UIImage *image = [UIImage imageWithData:decryptedData]; 

//adding image 
UIImageView *movingImageView = [[UIImageView alloc]initWithImage:image]; 
[movingImageView setContentMode:UIViewContentModeScaleAspectFit]; 
[self.scrollView addSubview:movingImageView]; 

現在movingImageView忽略了內容模式!如果我宣佈其框架,它將被修復!但問題是我的數據是不同的圖像與不同的width大小。任何解決方案

編輯: 實測值的溶液:

movingImageView.frame = CGRectMake(0, 0, image.size.width/2, image.size.height/2); 
+0

'initWithImage:'將視圖框設置爲圖像的大小,所有'contentMode'值都會有相同的效果。您需要分別爲「contentMode」設置框架以更改圖像的顯示方式。 – Mats

回答

0

根據文檔:

initWithImage:調整接收機的幀以匹配指定的圖像的大小。它還禁用攝像畫面的用戶交互由default.`

顯然contentMode將沒有任何效果,如果imageViewimage具有相同的大小。

我猜你需要的是指定imageViewwidth(可能與scrollView相同?),並根據image的尺寸計算高度。

0

使用以下幾行。它會將圖像填充到框架中。如果大圖像出現,它將顯示圖像的縱橫比和一些部分可能剪輯。嘗試一次。

movingImageView.contentMode = UIViewContentModeScaleAspectFill; 
movingImageView.clipsToBounds = YES;[self.scrollView addSubview:movingImageView]; 
+0

沒有什麼改變! –