我怎样才能让我的图像占据整个图像视图?
How would I get my image to occupy the whole image view?
我有一个设置了约束的图像视图,我加载的图像(来自 URL)出于某种原因没有填满整个图像视图。我包括了一张有问题的图片,然后是相同 'post' 的详细视图。两者使用相同的代码。在详细视图(正确显示的视图)中,我在 viewdidload 期间设置了 post 图像,它是一个独立视图。在 wonky 中加载的是一个 table 视图单元格,带有自定义 class,图像设置在 cellForRowAtIndexPath 中。在将图像发送到 Back4App 并解析为 500x500 之前,我确实调整了图像的大小,并且图像视图都设置为裁剪到边界和纵横比适合。 uiimageview 约束完全相同,水平对齐为零,宽度和高度固定,所有四个边的约束(距离最近的邻居 8)
以下是如何完成这两个操作的代码片段:
粉色背景:
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
PostCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"PostCell"];
Post *post = self.posts[indexPath.row];
PFUser *user = post.author;
[user fetchInBackgroundWithBlock:^(PFObject * _Nullable object, NSError * _Nullable error) {
cell.usernameLabel.text = post.author.username;
}];
cell.titleLabel.text = post.caption;
cell.isLiked = NO;
NSURL *imageURL = [NSURL URLWithString:post.image.url];
[cell.imageView setImageWithURL:imageURL];
return cell;
}
正确加载的:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
PFUser *user = self.post.author;
[user fetchInBackgroundWithBlock:^(PFObject * _Nullable object, NSError * _Nullable error) {
self.usernameTextLabel.text = self.post.author.username;
}];
self.titleTextLabel.text = self.post.caption;
self.descriptionTextLabel.text = self.post.bodyText;
NSURL *postImageURL = [NSURL URLWithString:self.post.image.url];
[self.postImageView setImageWithURL:postImageURL];'''
}
有什么建议吗?可能有一些我无法掌握的基础知识——我基本上参加了一个速成班,我只参加了大约 3 个星期,所以我对其中的很多理解并不像我希望的那样深入.
I've set the background color to pink so you could see the image size vs image view
one that loads correctly
旁注:我真的不知道如何正确地编写代码块???我在听它说的 哈哈
检查以下几点
调试您提供的正确 ImageView 的帧大小。
为ImageView设置以下内容
[cell.imageView setImageWithURL:imageURL];
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
[cell.imageView setClipsToBounds:YES];
我不小心使用了单元格附带的默认 imageView 而不是我自己的新图像视图出口。
我有一个设置了约束的图像视图,我加载的图像(来自 URL)出于某种原因没有填满整个图像视图。我包括了一张有问题的图片,然后是相同 'post' 的详细视图。两者使用相同的代码。在详细视图(正确显示的视图)中,我在 viewdidload 期间设置了 post 图像,它是一个独立视图。在 wonky 中加载的是一个 table 视图单元格,带有自定义 class,图像设置在 cellForRowAtIndexPath 中。在将图像发送到 Back4App 并解析为 500x500 之前,我确实调整了图像的大小,并且图像视图都设置为裁剪到边界和纵横比适合。 uiimageview 约束完全相同,水平对齐为零,宽度和高度固定,所有四个边的约束(距离最近的邻居 8)
以下是如何完成这两个操作的代码片段:
粉色背景:
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
PostCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"PostCell"];
Post *post = self.posts[indexPath.row];
PFUser *user = post.author;
[user fetchInBackgroundWithBlock:^(PFObject * _Nullable object, NSError * _Nullable error) {
cell.usernameLabel.text = post.author.username;
}];
cell.titleLabel.text = post.caption;
cell.isLiked = NO;
NSURL *imageURL = [NSURL URLWithString:post.image.url];
[cell.imageView setImageWithURL:imageURL];
return cell;
}
正确加载的:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
PFUser *user = self.post.author;
[user fetchInBackgroundWithBlock:^(PFObject * _Nullable object, NSError * _Nullable error) {
self.usernameTextLabel.text = self.post.author.username;
}];
self.titleTextLabel.text = self.post.caption;
self.descriptionTextLabel.text = self.post.bodyText;
NSURL *postImageURL = [NSURL URLWithString:self.post.image.url];
[self.postImageView setImageWithURL:postImageURL];'''
}
有什么建议吗?可能有一些我无法掌握的基础知识——我基本上参加了一个速成班,我只参加了大约 3 个星期,所以我对其中的很多理解并不像我希望的那样深入.
I've set the background color to pink so you could see the image size vs image view
one that loads correctly
旁注:我真的不知道如何正确地编写代码块???我在听它说的 哈哈
检查以下几点
调试您提供的正确 ImageView 的帧大小。
为ImageView设置以下内容
[cell.imageView setImageWithURL:imageURL]; cell.imageView.contentMode = UIViewContentModeScaleAspectFill; [cell.imageView setClipsToBounds:YES];
我不小心使用了单元格附带的默认 imageView 而不是我自己的新图像视图出口。