gpt4 book ai didi

objective-c - 如何在 IOS 中心水平对齐我的图像

转载 作者:技术小花猫 更新时间:2023-10-29 11:09:10 25 4
gpt4 key购买 nike

这是我的图片:

Obj *obj = ... obj has imageHref which is NSString

UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:obj.imageHref]];
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];

根据图像大小,我不知道如何将它居中,有什么想法吗?

这都是内部方法:

- (void)drawRect:(CGRect)rect {    
Obj *obj = ... obj has imageHref which is NSString

UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:obj.imageHref]];
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];

NSLog(@"%f", rect.size.width);
NSLog(@"%f", rect.size.height);
}

rect.size.width 打印 320.000000 而 rect.size.height 打印 416.000000

最佳答案

[image drawInRect:CGRectMake((self.view.frame.size.width/2) - (image.size.width/2),
(self.view.frame.size.height / 2) - (image.size.height / 2),
image.size.width,
image.size.height)];

编辑:

因为您已经在 UIView 中:

[image drawInRect:CGRectMake((self.frame.size.width/2) - (image.size.width/2), (self.frame.size.height / 2) - (image.size.height / 2), image.size.width, image.size.height)];

关于objective-c - 如何在 IOS 中心水平对齐我的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9484342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com