ios - 何时/如何设置 UIImageView contentMode(UIImageView setContentMode 不起作用)

标签 ios objective-c uiimageview uiimage contentmode

我不明白为什么我的 UIImageViews 中的 UIImages 没有正确地调整到 UIImageView 的大小。任何人都可以阐明我的代码中的问题吗?以下是我的 SidewalkPlusSuperCell 的相关代码,它是一个子类 UICollectionViewCell。我正在尝试从显示 UIImage(传单)的代码构建 UICollectionViewCell,但无法使传单 UIImage 适合/调整大小UIImageView 的大小。我知道我必须设置 contentMode,但无论我在哪里设置 contentMode,图像都保持未调整大小。

//
//  SidewalkPlusSuperCell.m
//

#import "SidewalkPlusSuperCell.h"
#import "Masonry.h"

@interface SidewalkPlusSuperCell()
@property (nonatomic) BOOL showInformation;
@property (strong, nonatomic) UIImageView *flyerImageView;
@end

@implementation SidewalkPlusSuperCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _showInformation = NO;
    }
    return self;
}

- (void)updateConstraints
{
    if (_showInformation) {
        UIEdgeInsets imagePlusTextPadding = UIEdgeInsetsMake(5, 5, 5, 5);
        [_flyerImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.superview.mas_top).with.offset(imagePlusTextPadding.top);
            make.leading.equalTo(self.superview.mas_leading).with.offset(imagePlusTextPadding.left);
            make.bottom.equalTo(self.superview.mas_bottom).with.offset(imagePlusTextPadding.bottom);
        }];

    } else {
        UIEdgeInsets onlyImagePadding = UIEdgeInsetsMake(0, 0, 0, 0);
        [_flyerImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.superview).with.insets(onlyImagePadding);
        }];
    }

    [super updateConstraints];
}

- (void)drawRect:(CGRect)rect
{
    if (_showInformation) {

    } else {
        _flyerImageView = [[UIImageView alloc] init];
        [_flyerImageView setFrame:rect];
        [_flyerImageView setContentMode:UIViewContentModeScaleAspectFit];
        [_flyerImageView setImage:self.flyerImage]; //self.flyerImage is a public property that is set by the UICollectionViewController
        [self addSubview:_flyerImageView];

        UIEdgeInsets onlyImagePadding = UIEdgeInsetsMake(0, 0, 0, 0);
        [_flyerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.superview).with.insets(onlyImagePadding);
        }];
        [self updateConstraints];
    }
}


@end

当我运行代码时,我得到以下信息:Caption

单元格应该有不同的大小(根据原始照片的长宽比调整大小),但照片应该调整大小以适合单元格。有没有想过为什么尽管我设置了 UIViewContentMode,但 UIImages 没有调整大小?

最佳答案

您可以尝试设置父 View clipsToBounds 吗?

如果image view在self.view什么时候做

 self.view.clipToBounds = YES;

关于ios - 何时/如何设置 UIImageView contentMode(UIImageView setContentMode 不起作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25255487/

相关文章:

iphone - 我可以为 NSNotifications 注册一个类吗?我可以将类方法与 NSNotifications 一起使用吗?

Objective-C Base64 解码返回 nil

ios - 如何在 Swift 3 中加速大量 UIImages

ios - 如何让我的应用程序在 iPhone 4 和 iPhone 5 屏幕上运行?

ios - 如何以编程方式将 UIImageView 缩放到固定宽度和灵活高度?

ios - 在同一 iOS 屏幕上同时显示两个视频

ios - Swift 4 中的 statusBarStyle

iphone - 将弧度角转换为 CGVector

ios - UITableViewCell addTarget 中的按钮不起作用

ios - 将 cocoapods 用于 facebookSDK 时找不到“FBSDKAppEvents.h”文件