2015-10-28 23 views
1

我嘗試將圖片處理我的UIImageView被稱爲imgBack 我定義我的頭UIImageView作爲IBOutletUIViewContentModeScaleAspectFit中的UIImageView工作不正常

IBOutlet UIImageView *imgBack; 

viewDidLoad功能我設置contentMode這樣:

imgBack.contentMode = UIViewContentModeScaleAspectFit; 

但經過我的圖像加載到imgBack這樣的:

imgBack.image = [UIImage imageWithContentsOfFile: imagePath]; 

它不適合以適合ImageView的

這是我加載到imgBack圖片: image i try to load

,這是它看起來像在視圖中:image in UIImageView

由於你看,圖像不適合視圖。 有誰知道爲什麼?

+0

使用'UIViewContentModeScaleAspectFill' –

+0

試一下吧。但隨後它剛剛裁剪。 – rockZ

+0

你爲imgBack設置了框架嗎? – Sana

回答

1

如果改變ImageView的contentMode不爲你工作,那麼你可以嘗試調整圖像大小,以適應與幫助圖片瀏覽的下面的代碼

UIImage *originalImage = [UIImage imageWithContentsOfFile: imagePath]; 
imgBack.image = [self resizeImage: originalImage imageSize: imgBack.size]; 

這個代碼添加到你的ViewController

-(UIImage*)resizeImage:(UIImage *)image imageSize:(CGSize)size 
    { 
     UIGraphicsBeginImageContext(size); 
     [image drawInRect:CGRectMake(0,0,size.width,size.height)]; 
     UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
     //here is the scaled image which has been changed to the size specified 
     UIGraphicsEndImageContext(); 
     return newImage; 

    } 
+0

感謝您的回答,但是它對我來說看起來像一個醜陋的解決方法。原始圖像是否被這個較小的圖像過度修改? – rockZ

+0

沒有你從這個方法中獲得一個單獨的UIImage,你可以在你的UIImageView中使用它。原始圖像完全沒有改變。 –

-2
below are the mode try and check 
UIViewContentModeScaleToFill, 
    UIViewContentModeScaleAspectFit,  // contents scaled to fit with fixed aspect. remainder is transparent 
    UIViewContentModeScaleAspectFill,  // contents scaled to fill with fixed aspect. some portion of content may be clipped. 
    UIViewContentModeRedraw,    // redraw on bounds change (calls -setNeedsDisplay) 
    UIViewContentModeCenter,    // contents remain same size. positioned adjusted. 
    UIViewContentModeTop, 
    UIViewContentModeBottom, 
    UIViewContentModeLeft, 
    UIViewContentModeRight, 
    UIViewContentModeTopLeft, 
    UIViewContentModeTopRight, 
    UIViewContentModeBottomLeft, 
    UIViewContentModeBottomRight, 
+0

我嘗試了其中大多數,但沒有任何作品,因爲它應該。我認爲應該工作的一個是AspectFit – rockZ

0

附上圖片UIImageView,然後再改變content mode

imgBack.image = [UIImage imageWithContentsOfFile: imagePath]; 
imgBack.contentMode = UIViewContentModeScaleAspectFill; 
imgBack.clipsToBounds = YES; 
+0

屬性'imageView'找不到'UIImageView *'類型的對象;你的意思是'maskView'? – rockZ