【问题标题】:how to fit the image in tableview based on size and Aspect ration?如何根据大小和纵横比将图像适合表格视图?
【发布时间】:2017-03-10 10:17:41
【问题描述】:

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

【问题讨论】:

标签: ios objective-c iphone swift


【解决方案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;

}

【讨论】:

    【解决方案2】:

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

    +(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;
    

    }

    【讨论】:

      猜你喜欢
      • 2013-07-28
      • 1970-01-01
      • 2020-05-10
      • 2014-03-15
      • 1970-01-01
      • 2017-08-10
      • 2012-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多