source

UI 이미지의 높이와 너비를 확인하려면 어떻게 해야 합니까?

nicesource 2023. 4. 9. 21:42
반응형

UI 이미지의 높이와 너비를 확인하려면 어떻게 해야 합니까?

메일의 URL 이미지에서

메일 보기에 이미지를 추가하는 중입니다.전체 이미지가 표시됩니다.하지만 이미지의 높이와 폭을 비례적으로 변화시켜 계산하고 싶습니다.

의 높이와 폭을 얻으려면 어떻게 해야 합니까?UIImage?

let heightInPoints = image.size.height
let heightInPixels = heightInPoints * image.scale

let widthInPoints = image.size.width
let widthInPixels = widthInPoints * image.scale

를 사용합니다.size속성을 지정합니다.상세한 것에 대하여는, 메뉴얼을 참조해 주세요.

UIImage *img = [UIImage imageNamed:@"logo.png"];

CGFloat width = img.size.width;
CGFloat height = img.size.height;

위의 일부 Anwers는 장치를 회전할 때 제대로 작동하지 않습니다.

시험:

CGRect imageContent = self.myUIImage.bounds;
CGFloat imageWidth = imageContent.size.width;
CGFloat imageHeight = imageContent.size.height;

유용한 솔루션은 많이 있지만 확장 기능을 통해 단순화할 수 있는 방법은 없습니다.확장자를 사용하여 문제를 해결하는 코드는 다음과 같습니다.

extension UIImage {

    var getWidth: CGFloat {
        get {
            let width = self.size.width
            return width
        }
    }

    var getHeight: CGFloat {
        get {
            let height = self.size.height
            return height
        }
    }
}
UIImageView *imageView = [[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MyImage.png"]]autorelease];

NSLog(@"Size of my Image => %f, %f ", [[imageView image] size].width, [[imageView image] size].height) ;
    import func AVFoundation.AVMakeRect

    let imageRect = AVMakeRect(aspectRatio: self.image!.size, insideRect: self.bounds)

    x = imageRect.minX

    y = imageRect.minY
let imageView: UIImageView = //this is your existing imageView

let imageViewHeight: CGFloat = imageView.frame.height

let imageViewWidth: CGFloat = imageView.frame.width

언급URL : https://stackoverflow.com/questions/5249664/how-can-i-get-the-height-and-width-of-an-uiimage

반응형