Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS:将UIImageView与顶部对齐_Ios_Objective C - Fatal编程技术网

iOS:将UIImageView与顶部对齐

iOS:将UIImageView与顶部对齐,ios,objective-c,Ios,Objective C,我有一个viewcontroller,我用它从网络上加载图像。图像通常会比视口大很多,因此我将缩小它的比例并将其放置在UIImageView中。问题是它不想与视口顶部对齐。我该如何解决这个问题。代码如下: - (void)viewDidLoad { NSURL *imageURL = [NSURL URLWithString:[[self exhibit] image]]; NSData *imageData = [NSData dataWithContentsOfURL:imageURL];

我有一个viewcontroller,我用它从网络上加载图像。图像通常会比视口大很多,因此我将缩小它的比例并将其放置在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是原始图像大小的大小,而不是缩小的版本。我假设这就是问题所在,但我不确定如何解决它。

我提出了另一个解决方案。我修改了此解决方案以适合我:

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

这是我的新视图

- (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];
}
-(void)viewDidLoad
{
NSURL*imageURL=[NSURL URLWithString:[[self exhibit]image]];
NSData*imageData=[NSData dataWithContentsOfURL:imageURL];
UIImage*image=[UIImage imageWithData:imageData];
如果([[UIScreen mainScreen]bounds].size.width<600){
CGSize newSize=CGSizeMake(300.0140.6);
UIGraphicsBeginImageContext(新闻大小);
[image drawInRect:CGRectMake(0.0,0.0,newSize.width,newSize.height)];
image=UIGraphicsGetImageFromCurrentImageContext();
}
NSLog(@“%@”、NSStringFromCGSize([图像大小]);
CGRect frame=CGRectMake(0.0,0.0,image.size.width,image.size.height);
UIImageView*iv=[[UIImageView alloc]initWithFrame:frame];
[四.图像:图像];
[iv setContentMode:UIViewContentModeScaleAspectFit];
[iv设置翻译自动调整大小GMaskintoConstraints:否];
[iv setBackgroundColor:[UIColor redColor];
[[self view]addSubview:iv];
[自我设置图像视图:iv];
[[self-imageView]setFrame:CGRectMake(0.0,0.0300.0140.6)];
NSDictionary*nameMap=@{@“imageView”:[self imageView]};
NSArray*水平约束=[NSLayoutConstraint Constraints WithVisualFormat:@“H:|-10-[imageView]-10-|”
选项:NSLayoutFormatAlignAllCenterX
指标:零
视图:名称映射];
[[self view]添加约束:水平约束];
NSArray*垂直约束=[NSLayoutConstraint Constraints WithVisualFormat:@“V:|-0-[imageView]”
选项:NSLayoutFormatAlignAllTop
指标:零
视图:名称映射];
[[self view]addConstraints:verticalConstraints];
}

好的,contentMode UIViewContentModeScaleAspectFit是图像视图与原始图像大小相同的原因。您可以尝试切换到UIViewContentModeScaleToFill(或者删除设置contentMode的行,因为UIViewContentModeScaleToFill是默认设置)。这不会忽略纵横比吗?是的,您是对的。我想我不完全确定问题是什么。您希望图像视图距离superview的上边缘为0像素?或者您希望图像视图的大小与图像的缩小版本相同?我希望图像缩小到视图中,并与视图顶部对齐。这有意义吗?您的自动布局约束有任何作用吗?我认为您希望视觉格式语言字符串中的视图为[iv]或[_imageView]。很抱歉所有的评论,但我真的不知道如何回答你的问题。