2015-09-28 136 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