使 UIImageView 适合 UIImage

Fit a UIImageView to a UIImage

我有一个使用 AutoLayout 和约束设置的 UIImage 视图。我使用

使图像适合图像视图
     self.selectPhoto.contentMode = UIViewContentModeScaleAspectFit;
     NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom relatedBy:0 toItem:self.selectPhoto attribute:NSLayoutAttributeTop multiplier:1 constant:-10];
     NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:self.selectPhoto attribute:NSLayoutAttributeBottom relatedBy:0 toItem:self.caption attribute:NSLayoutAttributeTop multiplier:1 constant:-35];
     [self.view addConstraint:topConstraint];
     [self.view addConstraint:bottomConstraint];
     self.selectPhoto.image= existingImage;

效果很好。因为 UIImageView 是使用 AutoLayout 设置的,而 UIImage 是使用 Aspect Fit 显示的,所以我不知道图像的确切框架。我希望图像的左上角在图像视图中位于 0,0,因为我有另一个较小的图像,用户可以在它上面移动。

例如:由于瀑布图像的方向,左上角在 UIImageView 中为 0,23ish。当图像高度 > 图像宽度时,(图像视图的)原点为 66,0。这是一个问题,因为我想绘制两个图像的用户上下文并另存为新图像。我不能这样做,因为我不知道 2 个月的图像在瀑布图像上的位置,因为我不知道瀑布图像的来源。我知道 2 个月的图像在图像视图中的位置,但图像并没有占据整个图像视图。由于 AutoLayout 和 Aspect Fit,它会根据图像的 size/orientation 而有所不同。我将 2Months Pan 手势添加到瀑布图像视图中,但起源不一致。附加到 2Months 视图的平移手势代码如下

-(void) handlePan:(UIPanGestureRecognizer *)recognizer {
    CGPoint translation = [recognizer translationInView:self.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
    self.itemToEdit.stickerLocation= recognizer.view.frame;
    NSLog(@"The location of the sticker is %f and y is %f", recognizer.view.frame.origin.x, recognizer.view.frame.origin.y);
}

有没有办法让 UIImageView 适合实际显示的 UIImage?基本上我想做类似 self.selectPhoto.frame = self.selectPhoto.image.frame 我做不到。

总结:在使用 AutoLayout 和 Aspect fit 放置 imageView 后,我如何知道它的绝对原点?

基于this Stack Overflow answer,我们可以通过访问其size 属性.

来获取UIImage的大小

因此,从这里开始,最简单的方法是使用自动版式。

使用图像视图设置故事板或 xib。继续,给你的图像视图一个明确的宽度和高度约束。现在,填写图像视图周围的其余约束。但是请记住,当我们将不同的图像放入图像视图时,图像视图上的显式 width/height 会发生变化,因此我们的其他自动布局约束必须牢记这一点。

现在,为我们的显式高度和宽度约束添加一个 IBOutlet。这可以像我们为任何其他 UI 元素添加出口一样完成。只需 Ctrl+ 从 IB 中的约束拖动到源文件。

现在我们有这样的东西:

@property (nonatomic, weak) NSLayoutConstraint *imageHeight;
@property (nonatomic, weak) NSLayoutConstraint *imageWidth;

现在,每次我们更改图像视图的图像时,我们都会更新这些约束的 constant 部分:

self.imageHeight.constant = newImage.size.height;
self.imageWidth.constant = newImage.size.width;

现在,使用上述方法意味着我们的图像视图将始终与我们的图像具有完全相同的大小。这可能意味着它延伸到屏幕边界之外或只填满屏幕的一小部分。

另一种方法是使用纵横比约束。再一次,首先给你的图像视图一个宽高比约束,将它的高度与它的宽度相关联。现在四处走走,为视图设置其余的自动布局约束。也许您希望将左上角固定到特定位置。

在执行此操作时,您可能还想为图像视图的宽度和高度设置一些小于或等于约束。这些约束将确保无论图像的大小或形状如何,图像视图都将小于您指定的大小(因此留在屏幕上)。

再次为您的纵横比约束添加一个出口。每次我们更改图像视图的图像时,我们都会修改它:

@property (nonatomic, weak) NSLayoutConstraint *imageRatio;

现在,我们将修改比率约束的 multiplier 属性,而不是 constant 属性(我们希望将其保留为零) .宽度是在顶部还是底部完全取决于约束的连接方式,因此可能需要反复试验。但它的要点是,我们想要这样的东西:

self.imageRatio.multiplier = newImage.size.height / newImage.size.width;

(同样,您可能需要在此处翻转 height/width。)