0

我有一个用于从网络加载图像的视图控制器。图像通常会比视口大得多,因此我将其缩小并将其放置在 UIImageView 中。问题是它不想与视口的顶部对齐。我将如何解决这个问题。这是代码:

- (void)viewDidLoad
{
NSURL *imageURL = [NSURL URLWithString:[[self exhibit] image]];

NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];

UIImageView *iv = [[UIImageView alloc] initWithImage:image];


[iv setContentMode:UIViewContentModeScaleAspectFit];
[iv setTranslatesAutoresizingMaskIntoConstraints:NO];
[[self view] addSubview:iv];
[self setImageView:iv];

NSDictionary *nameMap = @{@"imageView" : [self imageView]};

NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[imageView]-10-|"
                                                                         options:NSLayoutFormatAlignAllCenterX
                                                                         metrics:nil
                                                                           views:nameMap];
[[self view] addConstraints:horizontalConstraints];

NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[imageView]"
                                                                         options:NSLayoutFormatAlignAllTop
                                                                         metrics:nil
                                                                           views:nameMap];
[[self view] addConstraints:verticalConstraints];

NSLog(@"%@", NSStringFromCGRect([[self imageView] frame]));
}

最后的日志显示 UIImageView 是原始图像大小的大小而不是缩小版本。我假设这是问题所在,但我不确定如何解决它。

4

1 回答 1

0

我做了另一个解决方案。我修改了此解决方案以适合我: 使用 iPhone SDK 调整/优化图像大小的最简单方法是什么?

如果屏幕宽度小于 600 像素,我只需要修改图像。我也事先知道我将使用的图像的纵横比。

这是我的新 viewDidLoad

- (void)viewDidLoad
{
NSURL *imageURL = [NSURL URLWithString:[[self exhibit] image]];

NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];

if ([[UIScreen mainScreen] bounds].size.width < 600 ) {
    CGSize newSize = CGSizeMake(300.0, 140.6);
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0.0, 0.0, newSize.width, newSize.height)];
    image = UIGraphicsGetImageFromCurrentImageContext();
}

NSLog(@"%@",NSStringFromCGSize([image size]));

CGRect frame = CGRectMake(0.0, 0.0,image.size.width,image.size.height);
UIImageView *iv = [[UIImageView alloc] initWithFrame:frame];
[iv setImage:image];
[iv setContentMode:UIViewContentModeScaleAspectFit];
[iv setTranslatesAutoresizingMaskIntoConstraints:NO];
[iv setBackgroundColor:[UIColor redColor]];
[[self view] addSubview:iv];
[self setImageView:iv];

[[self imageView] setFrame:CGRectMake(0.0, 0.0, 300.0, 140.6)];


NSDictionary *nameMap = @{@"imageView" : [self imageView]};

NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[imageView]-10-|"
                                                                         options:NSLayoutFormatAlignAllCenterX
                                                                         metrics:nil
                                                                           views:nameMap];
[[self view] addConstraints:horizontalConstraints];

NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[imageView]"
                                                                         options:NSLayoutFormatAlignAllTop
                                                                         metrics:nil
                                                                           views:nameMap];
[[self view] addConstraints:verticalConstraints];
}
于 2013-06-21T18:04:44.733 回答