Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.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适配到UIImage_Ios_Objective C_Uiimageview_Autolayout - Fatal编程技术网

Ios 将UIImageView适配到UIImage

Ios 将UIImageView适配到UIImage,ios,objective-c,uiimageview,autolayout,Ios,Objective C,Uiimageview,Autolayout,我有一个UIImage视图,它是使用自动布局和约束设置的。我使用 self.selectPhoto.contentMode = UIViewContentModeScaleAspectFit; NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom relatedBy:0 to

我有一个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,它会根据图像的大小/方向而有所不同。我在瀑布图像视图中添加了2个月平移姿势,但原点没有对齐。附在2个月视图上的平移手势代码如下

-(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后,如何知道它的绝对原点?

基于,我们可以通过访问其
size
属性来获取
UIImage
的大小

因此,从这里开始,最简单的方法就是使用autolayout

设置您的故事板或xib,并在其上显示您的图像视图。继续,为图像视图提供明确的宽度和高度约束。现在,填充图像视图周围的其余约束。但是,请记住,当我们将不同的图像放入图像视图时,图像视图上的显式宽度/高度将发生变化,因此其他自动布局约束必须记住这一点

现在,为我们明确的高度和宽度约束添加一个
IBOutlet
。这可以通过为任何其他UI元素添加出口的方式来完成。只需按住Ctrl键并将其从IB中的约束拖动到源文件

现在我们有了这样的东西:

@property (nonatomic, weak) NSLayoutConstraint *imageHeight;
@property (nonatomic, weak) NSLayoutConstraint *imageWidth;
self.imageRatio.multiplier = newImage.size.height / newImage.size.width;
现在,每次更改图像视图的图像时,我们都会更新这些约束的
常量
部分:

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

现在,使用上述方法意味着我们的图像视图将始终具有与图像完全相同的大小。这可能意味着它超出了屏幕的边界,或者只占据了屏幕的一小部分

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

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

再次为纵横比约束添加一个出口。每次更改图像视图的图像时,我们都将对此进行修改:

@property (nonatomic, weak) NSLayoutConstraint *imageRatio;
现在,我们将修改比率约束的
multiplier
属性,而不是
constant
属性(我们希望将其保留为零)。宽度是在顶部还是底部完全取决于约束的连接方式,因此可能需要一些尝试和错误。但要点是,我们想要这样的东西:

@property (nonatomic, weak) NSLayoutConstraint *imageHeight;
@property (nonatomic, weak) NSLayoutConstraint *imageWidth;
self.imageRatio.multiplier = newImage.size.height / newImage.size.width;

(同样,您可能需要在此处翻转高度/宽度。)

在使用AspectFit在imageView中设置图像之前,我不会调整图像大小。当我重新分配NSLayoutConstraint self.imageHeight.constant=newImage.size.height时;imageHeight似乎成为原始图像高度。重新指定imageWidth(约束)之前=288,imageWidth(约束)之后=1500。这些约束似乎是调整到实际图像的大小,而不是显示的图像。有什么想法吗?详细答案请点击Thx!尝试我使用纵横比详细介绍的第二种方法。