2015-10-28 79 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