如何根据大小和面积比例在tableview中拟合图像?

时间:2017-03-10 10:17:41

标签: ios objective-c iphone swift

我正在开发项目,如Instagram上传图像并显示主页中的所有Feed。但图像不适合图像视图。在Instagram表视图单元格中根据图像大小自动调整,请提供建议。

2 个答案:

答案 0 :(得分:1)

[_imageView setImage:[ViewController imageWithContentMode:[UIImage imageNamed:@"howwehelp_bg.png"] withScaledSize:_imageView.bounds.size]];

_imageView.contentMode = UIViewContentModeScaleToFill;

+(UIImage *)imageWithContentMode:(UIImage *)image withScaledSize:(CGSize)newSize{

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);

[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];

UIImage *imageNew = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return imageNew;

}

答案 1 :(得分:0)

您可以将此代码应用于合适的图像。

+(UIImage*)imageWithImage: (UIImage*) sourceImage scaledToWidth: (float) i_width
{
float oldWidth = sourceImage.size.width;
float scaleFactor = i_width / oldWidth;

float newHeight = sourceImage.size.height * scaleFactor;
float newWidth = oldWidth * scaleFactor;

UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight));
[sourceImage drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
UIGraphicsEndImageContext();
return newImage;

}