【问题标题】:iOS: ImageView filled with a pictureiOS:用图片填充的ImageView
【发布时间】:2014-02-25 11:54:16
【问题描述】:

我有一个包含图片路径的表格,保存在用户的库中。然后,我将此路径传递给仅包含 imageView 的 View。我想用图片填充这个 ImageView。像 WhatsApp(当您单击个人资料的图片时)。但是,我的照片总是被裁剪或扭曲。我尝试了不同的方法来做到这一点,但我没有找到最好的方法:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationController.navigationBar.translucent = NO;

    UIImage *picture = [UIImage imageWithContentsOfFile: self.picturePath]; //I passed this path from the previous View
    UIImageView* imageView = [[UIImageView alloc] initWithImage:picture];

    CGRect screenRect = [[UIScreen mainScreen] bounds];

    imageView.frame = CGRectMake(0, 0, screenRect.size.width, screenRect.size.height);
    imageView.contentMode = UIViewContentModeScaleAspectFill;

    [self.view addSubview:imageView];
}

原创(我想要类似的)

我的应用

【问题讨论】:

  • 您能否展示一下它的外观截图,并更好地描述您期望它的外观?
  • @CoolMonster 我尝试了内容模式的所有选项
  • 这可能会有所帮助:stackoverflow.com/questions/7838699/…
  • 这很好!可以帮助我,但不能解决问题。我已经尝试旋转图片了。
  • 您无法获得第一张图片上的内容,因为它与手机屏幕的纵横比不同,即使在横向模式下也不行。在这种情况下,您要么使图像比手机屏幕大一点,以便它在两个维度上填充它,然后将其裁剪为屏幕尺寸,要么让它完全适合,然后在一个维度上得到黑条。那么你更喜欢这两者中的哪一个呢?

标签: ios objective-c


【解决方案1】:

如果您的图像被剪切,原因是您在较小的容器中使用更高分辨率的图像并指示您的图像视图填充,使用此行imageView.contentMode = UIViewContentModeScaleAspectFill;

改成imageView.contentMode = UIViewContentModeScaleAspectFit;

【讨论】:

  • 我这样做了,现在图像很小。
【解决方案2】:

我认为@whitewolf09 是对的,他的方法会解决你的问题。但我建议您查看MGImageUtilities,这对于您可以裁剪图像保持纵横比以适合您的图像视图框架的不同情况非常有用。

  1. 只需#import "UIImage+ProportionalFill.h"
  2. UIImage *image = [[UIImage imageWithContentsOfFile:filePath] imageScaledToFitSize:imageView.frame.size];

这是用于调整图像大小的非常有用的类别,将来可能会对您有所帮助。

【讨论】:

    【解决方案3】:

    我认为它被剪裁的原因是您正在使用图片创建图像视图,然后设置框架并且可能图片比框架大,所以它正在剪裁它。还可以尝试将方面更改为:'UIViewContentModeScaleAspectFit' 试试这个:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.navigationController.navigationBar.translucent = NO;
    
        UIImage *picture = [UIImage imageWithContentsOfFile:self.picturePath]; //I passed this path from the previous View
        UIImageView* imageView = [[UIImageView alloc] initWithRect:CGRectMake(0, 0, screenRect.size.width, screenRect.size.height);];
    
        CGRect screenRect = [[UIScreen mainScreen] bounds];
    
        imageView.image = picture;
        imageView.contentMode = UIViewContentModeCenter;
    
        [self.view addSubview:imageView];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-28
      • 1970-01-01
      • 2016-07-18
      • 1970-01-01
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      相关资源
      最近更新 更多