I have a UIImage View that is set up using AutoLayout and constraints. I fit the image to the Image View using
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. Because the UIImageView is set up using AutoLayout, and the UIImage is shown using Aspect Fit, I do not know the exact frame of the image. I want the upper left hand corner of the image to be at 0,0 in the image view because I have another smaller image which the user can move around on top of it.
For example : Because of the orientation of the waterfall image the upper left hand corner is 0,23ish in the UIImageView. When the image height > image width the origin (of the image view) is 66,0. This is a problem because I want to then draw the user 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 image is on the image VIEW, but the image doesn't take up the entire imageView. Because of the AutoLayout and Aspect Fit it differs depending on the size/orientation of the image. I added the 2Months Pan gesture to the waterfall image view, but the origins don't line up. The code for the pan 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 can't do.
In summary : How do I know the absolute origin of my imageView after it has been placed using AutoLayout and Aspect fit?