将图像拟合到UAModalPanel



我目前正在使用UAModalPanel,我只想用这个组件显示图片。所以我做了以下事情:

#import <UIKit/UIKit.h>
#import "UAModalPanel.h"
@interface LolcatPanel : UAModalPanel
- (void)showLolcat;
@end

#import "LolcatPanel.h"
@implementation LolcatPanel
- (void)showLolcat
{
    int pic = (arc4random() % 10) + 1;
    NSString *intString = [NSString stringWithFormat:@"%d", pic];
    NSMutableString *picture = [[NSMutableString alloc] initWithString:intString];
    [picture appendString:@".jpg"];
    NSLog(@"Loading image %@", picture);
    NSString* imageName = [[NSBundle mainBundle] pathForResource:intString ofType:@"jpg"];
    UIImage *testImage = [UIImage imageWithContentsOfFile:imageName];
    if (testImage == nil) {
        NSLog(@"FAILED");
    }
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    imgView.image = testImage;
    imgView.contentMode = UIViewContentModeScaleToFill;
    [self addSubview:imgView];
}
@end

我想展示适合UAModelPanel的图片,但ATM的结果并不是很好。

检查图像视图显示时的大小。您还应该配置自动调整大小/自动布局,以便它对正在更改的超级视图有已知的反应。

您可能希望切换到纵横比匹配而不是缩放填充内容模式,以获得良好的显示效果。您可能还需要将图像视图设置为clipToBounds,以防止在设置的边界之外绘制任何图像。

我找到了解决方案:

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(22, 20, width, height)];
imgView.image = testImage;
imgView.contentMode = UIViewContentModeScaleAspectFit;
[self.contentContainer addSubview:imgView];

我不得不将imageview添加到内容容器中。

谨致问候,Sascha

最新更新