Install UIImageView to UIImage

I have a UIImage View that is configured using AutoLayout and constraints. I fit image to image with

     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;

      

It works great. Since the UIImageView is set up using AutoLayout and the UIImage is displayed using Aspect Fit, I don't know the exact frame of the image. I want the top left corner of the image to be at 0.0 in the image view because I have an even smaller image that the user can move on top of it.Image 1

For example: due to the orientation of the waterfall image, the top left corner is 0.23ish in the UIImageView. When image height> image width, the (image) origin is 66.0. This is a problem because I want to then draw the custom context of both images and save as a new image. I can't do this because I don't know where the 2 months image is placed on the waterfall image because I don't know the origin of the waterfall image. I know where the 2 months old image is in the VIEW image, but the image doesn't take up the whole imageView. Due to AutoLayout and Aspect Fit, it differs depending on the size / orientation of the image. I added a 2Months Pan gesture to the waterfall image view, but the origins don't line up. The code for the panning gesture,which is attached to the 2Months view is as follows

-(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);
}

      

Is there a way to fit the UIImageView to the actual displayed UIImage? Basically I want to do something like self.selectPhoto.frame = self.selectPhoto.image.frame

which I cannot do.

In short: How can I find out the absolute origin of my ImageView after placing it using AutoLayout and Aspect?

+3


source to share


1 answer


Based on this Stack Overflow answer , we can get the size UIImage

by accessing the property size

.

So, from here, the easiest way to proceed is to use autorun.

Customize your storyboard or xib with an image on it. Go ahead and give your image an explicit width and height constraint. Now fill in the rest of the constraints around the image. Keep in mind, however, that this explicit image width / height will change as we cast different images into the image, so our other auto-detect constraints must account for this.

Now add IBOutlet

for our explicit height and width constraints. This can be done in the same way as we add an output for any other interface element. Just Ctrl+ drag from constraint in IB to source file.

We now have something like this:

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

      

Now, every time we change the image of the image, we update some of constant

these constraints:

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

      




Now, using the approach described above, our image will always be the same size as our image. This could mean that it extends far beyond the screen or fills only a small portion of the screen.

An alternative approach would be to use aspect ratio limitation. Once again, start by giving your image view an aspect ratio constraint, linking its height to width. Now go ahead and configure the rest of the autodetect constraints for the view. Perhaps you want the top left corner anchored in a specific location.

While you are doing this, you will also want to set constraints less than or equal to the image width and height constraints. These limits will ensure that regardless of the size or shape of the image, the image will be smaller than your specified size (and therefore will remain on screen).

Add the outlet again to limit the aspect ratio. We will change this every time we change the image of the image:

@property (nonatomic, weak) NSLayoutConstraint *imageRatio;

      

Now, instead of the property constant

(we want to leave this at zero), we will change the multiplier

constraint property of the relationship. Whether the width is at the top or bottom depends on how the constraint is anchored, so it might take some trial and error. But the bottom line is that we want something like this:

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

      

(Again, you might have to flip the height / width here.)

+2


source







All Articles