作者热门文章
- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
这是我的图片:
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/
我是一名优秀的程序员,十分优秀!