Getting width and height of image from UIImageView after aspect fit
I'm trying to get the width and height of an image from UIImageView after it gets scaled to aspect fit. This is how I got its width and height:
let imageHeight = AVMakeRect(aspectRatio: (transferImage.size), insideRect: imageView.frame).height
let imageWidth = AVMakeRect(aspectRatio: (transferImage.size), insideRect: imageView.frame).width
let imageYposition = AVMakeRect(aspectRatio: (transferImage.size), insideRect: imageView.frame).origin.y
let imageXposition = AVMakeRect(aspectRatio: (transferImage.size), insideRect: imageView.frame).origin.x
Apparently, this gives me the width and height. However, after trying to size an UIView to the same dimensions, they are not the same size. This is how I set the UIView to the width and height of the image:
boundariesRectangle.frame = CGRect(x: imageXposition, y: imageYposition, width: imageWidth, height: imageHeight)
This is what happens:
swift
add a comment |
I'm trying to get the width and height of an image from UIImageView after it gets scaled to aspect fit. This is how I got its width and height:
let imageHeight = AVMakeRect(aspectRatio: (transferImage.size), insideRect: imageView.frame).height
let imageWidth = AVMakeRect(aspectRatio: (transferImage.size), insideRect: imageView.frame).width
let imageYposition = AVMakeRect(aspectRatio: (transferImage.size), insideRect: imageView.frame).origin.y
let imageXposition = AVMakeRect(aspectRatio: (transferImage.size), insideRect: imageView.frame).origin.x
Apparently, this gives me the width and height. However, after trying to size an UIView to the same dimensions, they are not the same size. This is how I set the UIView to the width and height of the image:
boundariesRectangle.frame = CGRect(x: imageXposition, y: imageYposition, width: imageWidth, height: imageHeight)
This is what happens:
swift
You can set it manually in your storyboard. It's easier that way
– Alex Tănăsescu
Nov 25 '18 at 9:27
add a comment |
I'm trying to get the width and height of an image from UIImageView after it gets scaled to aspect fit. This is how I got its width and height:
let imageHeight = AVMakeRect(aspectRatio: (transferImage.size), insideRect: imageView.frame).height
let imageWidth = AVMakeRect(aspectRatio: (transferImage.size), insideRect: imageView.frame).width
let imageYposition = AVMakeRect(aspectRatio: (transferImage.size), insideRect: imageView.frame).origin.y
let imageXposition = AVMakeRect(aspectRatio: (transferImage.size), insideRect: imageView.frame).origin.x
Apparently, this gives me the width and height. However, after trying to size an UIView to the same dimensions, they are not the same size. This is how I set the UIView to the width and height of the image:
boundariesRectangle.frame = CGRect(x: imageXposition, y: imageYposition, width: imageWidth, height: imageHeight)
This is what happens:
swift
I'm trying to get the width and height of an image from UIImageView after it gets scaled to aspect fit. This is how I got its width and height:
let imageHeight = AVMakeRect(aspectRatio: (transferImage.size), insideRect: imageView.frame).height
let imageWidth = AVMakeRect(aspectRatio: (transferImage.size), insideRect: imageView.frame).width
let imageYposition = AVMakeRect(aspectRatio: (transferImage.size), insideRect: imageView.frame).origin.y
let imageXposition = AVMakeRect(aspectRatio: (transferImage.size), insideRect: imageView.frame).origin.x
Apparently, this gives me the width and height. However, after trying to size an UIView to the same dimensions, they are not the same size. This is how I set the UIView to the width and height of the image:
boundariesRectangle.frame = CGRect(x: imageXposition, y: imageYposition, width: imageWidth, height: imageHeight)
This is what happens:
swift
swift
edited Nov 25 '18 at 17:01
Code Different
47.2k776109
47.2k776109
asked Nov 25 '18 at 9:25
M. ZhongM. Zhong
61
61
You can set it manually in your storyboard. It's easier that way
– Alex Tănăsescu
Nov 25 '18 at 9:27
add a comment |
You can set it manually in your storyboard. It's easier that way
– Alex Tănăsescu
Nov 25 '18 at 9:27
You can set it manually in your storyboard. It's easier that way
– Alex Tănăsescu
Nov 25 '18 at 9:27
You can set it manually in your storyboard. It's easier that way
– Alex Tănăsescu
Nov 25 '18 at 9:27
add a comment |
1 Answer
1
active
oldest
votes
Try this:
guard let image = imageView.image else { return }
// Calculate the scaled size of the image
let scaledRect = AVMakeRect(aspectRatio: image.size, insideRect: imageView.bounds)
// Create the overlay view
let overlayView = UIView(frame: .zero)
overlayView.backgroundColor = UIColor.red.withAlphaComponent(0.5)
overlayView.translatesAutoresizingMaskIntoConstraints = false
// Add the overlay
// To use relate 2 views by auto-layout constraints, they must have the
// same parent view
imageView.superView!.addSubview(overlayView)
overlayView.widthAnchor.constraint(equalToConstant: scaledRect.width).isActive = true
overlayView.heightAnchor.constraint(equalToConstant: scaledRect.height).isActive = true
overlayView.centerXAnchor.constraint(equalTo: imageView.centerXAnchor).isActive = true
overlayView.centerYAnchor.constraint(equalTo: imageView.centerYAnchor).isActive = true
Still not the same size. This is what it looks like: imgur.com/a/Rhkra8k
– M. Zhong
Nov 26 '18 at 1:05
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466174%2fgetting-width-and-height-of-image-from-uiimageview-after-aspect-fit%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this:
guard let image = imageView.image else { return }
// Calculate the scaled size of the image
let scaledRect = AVMakeRect(aspectRatio: image.size, insideRect: imageView.bounds)
// Create the overlay view
let overlayView = UIView(frame: .zero)
overlayView.backgroundColor = UIColor.red.withAlphaComponent(0.5)
overlayView.translatesAutoresizingMaskIntoConstraints = false
// Add the overlay
// To use relate 2 views by auto-layout constraints, they must have the
// same parent view
imageView.superView!.addSubview(overlayView)
overlayView.widthAnchor.constraint(equalToConstant: scaledRect.width).isActive = true
overlayView.heightAnchor.constraint(equalToConstant: scaledRect.height).isActive = true
overlayView.centerXAnchor.constraint(equalTo: imageView.centerXAnchor).isActive = true
overlayView.centerYAnchor.constraint(equalTo: imageView.centerYAnchor).isActive = true
Still not the same size. This is what it looks like: imgur.com/a/Rhkra8k
– M. Zhong
Nov 26 '18 at 1:05
add a comment |
Try this:
guard let image = imageView.image else { return }
// Calculate the scaled size of the image
let scaledRect = AVMakeRect(aspectRatio: image.size, insideRect: imageView.bounds)
// Create the overlay view
let overlayView = UIView(frame: .zero)
overlayView.backgroundColor = UIColor.red.withAlphaComponent(0.5)
overlayView.translatesAutoresizingMaskIntoConstraints = false
// Add the overlay
// To use relate 2 views by auto-layout constraints, they must have the
// same parent view
imageView.superView!.addSubview(overlayView)
overlayView.widthAnchor.constraint(equalToConstant: scaledRect.width).isActive = true
overlayView.heightAnchor.constraint(equalToConstant: scaledRect.height).isActive = true
overlayView.centerXAnchor.constraint(equalTo: imageView.centerXAnchor).isActive = true
overlayView.centerYAnchor.constraint(equalTo: imageView.centerYAnchor).isActive = true
Still not the same size. This is what it looks like: imgur.com/a/Rhkra8k
– M. Zhong
Nov 26 '18 at 1:05
add a comment |
Try this:
guard let image = imageView.image else { return }
// Calculate the scaled size of the image
let scaledRect = AVMakeRect(aspectRatio: image.size, insideRect: imageView.bounds)
// Create the overlay view
let overlayView = UIView(frame: .zero)
overlayView.backgroundColor = UIColor.red.withAlphaComponent(0.5)
overlayView.translatesAutoresizingMaskIntoConstraints = false
// Add the overlay
// To use relate 2 views by auto-layout constraints, they must have the
// same parent view
imageView.superView!.addSubview(overlayView)
overlayView.widthAnchor.constraint(equalToConstant: scaledRect.width).isActive = true
overlayView.heightAnchor.constraint(equalToConstant: scaledRect.height).isActive = true
overlayView.centerXAnchor.constraint(equalTo: imageView.centerXAnchor).isActive = true
overlayView.centerYAnchor.constraint(equalTo: imageView.centerYAnchor).isActive = true
Try this:
guard let image = imageView.image else { return }
// Calculate the scaled size of the image
let scaledRect = AVMakeRect(aspectRatio: image.size, insideRect: imageView.bounds)
// Create the overlay view
let overlayView = UIView(frame: .zero)
overlayView.backgroundColor = UIColor.red.withAlphaComponent(0.5)
overlayView.translatesAutoresizingMaskIntoConstraints = false
// Add the overlay
// To use relate 2 views by auto-layout constraints, they must have the
// same parent view
imageView.superView!.addSubview(overlayView)
overlayView.widthAnchor.constraint(equalToConstant: scaledRect.width).isActive = true
overlayView.heightAnchor.constraint(equalToConstant: scaledRect.height).isActive = true
overlayView.centerXAnchor.constraint(equalTo: imageView.centerXAnchor).isActive = true
overlayView.centerYAnchor.constraint(equalTo: imageView.centerYAnchor).isActive = true
answered Nov 25 '18 at 17:55
Code DifferentCode Different
47.2k776109
47.2k776109
Still not the same size. This is what it looks like: imgur.com/a/Rhkra8k
– M. Zhong
Nov 26 '18 at 1:05
add a comment |
Still not the same size. This is what it looks like: imgur.com/a/Rhkra8k
– M. Zhong
Nov 26 '18 at 1:05
Still not the same size. This is what it looks like: imgur.com/a/Rhkra8k
– M. Zhong
Nov 26 '18 at 1:05
Still not the same size. This is what it looks like: imgur.com/a/Rhkra8k
– M. Zhong
Nov 26 '18 at 1:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466174%2fgetting-width-and-height-of-image-from-uiimageview-after-aspect-fit%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can set it manually in your storyboard. It's easier that way
– Alex Tănăsescu
Nov 25 '18 at 9:27