简体   繁体   English

获取UI对象中宽度和高度的数字像素?

[英]Getting the number pixels in width and height inside an UI Object?

Currently I do something that looks a bit fuzzy because I am dealing with points with my UI Object but what I want to do is get the width and height of the UI Object (in my case a UIImageView) in pixels. 目前,我正在做一些看起来有些模糊的事情,因为我正在用UI Object处理点,但是我想做的是以像素为单位获取UI Object(在我的情况下为UIImageView)的宽度和高度。

Is this possible? 这可能吗? I have looked around the documentation but I have not seen anything that looks relevant. 我浏览了文档,但没有发现任何相关的内容。

Can anyone assist with this? 有人可以协助吗?

Thanks! 谢谢!

Have you tried this? 你有尝试过吗?

[object frame].size.height

[object frame].size.width

I'm pretty sure that anything with a visual representation will have a frame attribute to indicate where it's located. 我很确定任何具有视觉表示的东西都将具有frame属性来指示其位置。 Of course, the origin of an object's frame is relative to its container, but the size should always be useable. 当然,对象框架的origin是相对于其容器的,但size应始终可用。

Edit/Update: 编辑/更新:

I misread the initial question, which wants to also convert points --> pixels. 我误读了最初的问题,该问题也想转换点->像素。 Here's how to make sure you get that correctly: 这是确保正确获得的方法:

float scale;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    // This message was implemented in iOS 4.0
    scale = [[UIScreen mainScreen] scale];
} else {
    // Anything that is running < 4.0 doesn't have a Retina display
    scale = 1.0;
}

Then, multiply the height and width values by scale to get actual pixel count. 然后,将高度和宽度值乘以比例以获得实际像素数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CGBitmapContextCreate对于宽度和高度的某些像素返回NULL - CGBitmapContextCreate returns NULL for some pixels of width and height 如何以像素为单位设置 UIVIew 的宽度和高度? - how to set width and height of a UIVIew in pixels? 如何获得物体的高度和宽度? - How to get height and width of the object? 目标C:准确地获得UIView的高度和宽度 - Objective C : Getting the height and width of a UIView accurately 在iOS中使用相机的物体的高度和宽度 - Height and width of object using camera in iOS 如何相对于iOS中的图像动态更改UI按钮的宽度和高度 - how to change UI Button width and height dynamically with respect to image in ios UICollectionView基于项目内部的动态宽度吗? - UICollectionView dynamic width based on number of items inside it? 为纵向和横向模式获得相同的屏幕宽度和高度 - Getting same screen width and height for portrait and landscape mode 如何根据UITableView中的单元格数更改UITableViewCell中的UItableView的高度 - How to change height of UItableView inside UITableViewCell based on number of cells in the UITableView iOS UIView,动态高度取决于内部元素的数量 - iOS UIView with dynamic height depending on number of elements inside
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM