OGeek|极客世界-中国程序员成长平台

标题: iOS:在 UIView 的中心绘制圆形的 UIImage [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:26
标题: iOS:在 UIView 的中心绘制圆形的 UIImage

我有一个半径为 50 的圆形图像,现在我想将圆的中心与我的 View 的中心相同,如下所示:

- (void)drawRectCGRect)rect {
    UIImage *arrow = [UIImage imageWithPDFNamed"circle" atSize:CGSizeMake(50, 50)];
    [arrow drawAtPoint:self.center];
}

但是 drawAtPoint 不是在中心绘制。 我想知道如何定位图像的中心?这样我可以画吗?



Best Answer-推荐答案


来自 Apple 的 UIImage 文档

This method draws the entire image in the current graphics context, respecting the image’s orientation setting. In the default coordinate system, images are situated down and to the right of the specified point. This method respects any transforms applied to the current graphics context, however.

因此,将中心偏移一半的图像宽度和高度,以在 View 中心绘制图像的中心。

- (void)drawRectCGRect)rect {
    CGSize imageSize = CGSizeMake(50,50);
    UIImage *arrow = [UIImage imageWithPDFNamed"circle" atSize:imageSize];
    [arrow drawAtPoint:CGPointMake(self.center.x-imageSize.width/2., self.center.y-imageSize.height/2.)];
}

关于iOS:在 UIView 的中心绘制圆形的 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29073786/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4