ios - UIImageView 中的反别名 UIImage

标签 ios objective-c uiimageview uiimage antialiasing

我在屏幕上有一个 UIImageView,并且那个 UIImageView 的背景色是蓝色的——所以是一个蓝色的正方形。

我有一个 UIImage,它是透明背景上的白色形状。这只是两种颜色 - 透明和白色。图像中没有抗锯齿,因此看起来参差不齐。

我想做的是将图像放在 ImageView 中,但要抗锯齿。如果我只是把它放在 ImageView 中

myimageview.image=myImage;

然后我得到一个蓝色方 block ,里面有我的白色形状,但它没有抗锯齿,即将白色混合到蓝色中,所以它看起来很漂亮和光滑。我只是在蓝色背景上得到图像,我猜它应该做什么。

所以我的问题是,是否可以将图像添加到 uiimageview 中,但是否使用抗锯齿?

谢谢

最佳答案

不,至少没有一些复杂的代码来操作图像数据。您开始使用的图像只是缺少流畅显示所需的细节。

但是,您可能需要考虑在运行时绘制图像。下面是一个绘图方法的示例以及将其转换为 UIImage 的简单方法。这有几个优点,包括更小的文件大小和任何 iOS 设备上的最佳分辨率。

有一些工具可以帮助生成类似的代码。 PaintCode 是我的首选工具。

static UIImage* _imageOfStarShape = nil;

+ (void)drawStarShape
{
    //// Star Drawing
    UIBezierPath* starPath = UIBezierPath.bezierPath;
    [starPath moveToPoint: CGPointMake(50, 22.5)];
    [starPath addLineToPoint: CGPointMake(58.99, 35.62)];
    [starPath addLineToPoint: CGPointMake(74.25, 40.12)];
    [starPath addLineToPoint: CGPointMake(64.55, 52.73)];
    [starPath addLineToPoint: CGPointMake(64.99, 68.63)];
    [starPath addLineToPoint: CGPointMake(50, 63.3)];
    [starPath addLineToPoint: CGPointMake(35.01, 68.63)];
    [starPath addLineToPoint: CGPointMake(35.45, 52.73)];
    [starPath addLineToPoint: CGPointMake(25.75, 40.12)];
    [starPath addLineToPoint: CGPointMake(41.01, 35.62)];
    [starPath closePath];
    [UIColor.whiteColor setFill];
    [starPath fill];
}

+ (UIImage*)imageOfStarShape
{
    if (_imageOfStarShape)
        return _imageOfStarShape;

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(100, 100), NO, 0.0f);
    [ThisClass drawStarShape];

    _imageOfStarShape = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return _imageOfStarShape;
}

关于ios - UIImageView 中的反别名 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27568670/

相关文章:

iphone - UIActivityIndi​​catorView 不消失

objective-c - UIImageView 的 UIImage 为零

ios - UIImageView 内容模式

ios - 在 iOS 设置中显示自定义应用存储信息

objective-c - 在预编译头文件中定义常量——如何避免重复

ios - UIWebview 键盘返回键按下 ios 7

objective-c - iOS App在调用方法时等待HTTP响应?

ios - 对 UIImageView 从起点到终点的移动进行动画处理

ios - Xcode 7.0.1 导致 Linker Command Failed with Exit code 1 错误

iphone - 是否有针对 goto 的性能原因?