Getting width and height of image from UIImageView after aspect fit












1















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:



This is what happens










share|improve this question

























  • You can set it manually in your storyboard. It's easier that way

    – Alex Tănăsescu
    Nov 25 '18 at 9:27
















1















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:



This is what happens










share|improve this question

























  • You can set it manually in your storyboard. It's easier that way

    – Alex Tănăsescu
    Nov 25 '18 at 9:27














1












1








1


0






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:



This is what happens










share|improve this question
















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:



This is what happens







swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 17:01









Code Different

48k776112




48k776112










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



















  • 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












1 Answer
1






active

oldest

votes


















0














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





share|improve this answer
























  • Still not the same size. This is what it looks like: imgur.com/a/Rhkra8k

    – M. Zhong
    Nov 26 '18 at 1:05











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


}
});














draft saved

draft discarded


















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









0














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





share|improve this answer
























  • Still not the same size. This is what it looks like: imgur.com/a/Rhkra8k

    – M. Zhong
    Nov 26 '18 at 1:05
















0














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





share|improve this answer
























  • Still not the same size. This is what it looks like: imgur.com/a/Rhkra8k

    – M. Zhong
    Nov 26 '18 at 1:05














0












0








0







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 17:55









Code DifferentCode Different

48k776112




48k776112













  • 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





Still not the same size. This is what it looks like: imgur.com/a/Rhkra8k

– M. Zhong
Nov 26 '18 at 1:05




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'