如何制作一个小的uiimage并在其上手动设置像素?

发布于 2024-10-31 20:52:59 字数 99 浏览 2 评论 0原文

对于阴影,我想制作一个 1px x 2px UIImage 并手动将像素设置为某些 RGBA 值。

我该怎么做?我不想用小 png 污染我的包。

谢谢!

For a drop shadow, i want to make a 1px x 2px UIImage and manually set the pixels to certain RGBA values.

How can i do this? I'd rather not pollute my bundle with tiny png's.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧话新听 2024-11-07 20:52:59

这是一种方法。首先分配用 RGB 值填充的内存,然后调用“imageWithBytes”函数。

您分配的内存应该是 4*宽度*高度字节。每个像素由四个字节描述,顺序为 A、R、G、B。

完成后不要忘记释放内存!

+ (UIImage*)imageWithBytes:(unsigned char*)data size:(CGSize)size
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    if (colorSpace == NULL)
    {
        NSLog(@"Could not create color space");
        return nil;
    }

    CGContextRef context = CGBitmapContextCreate(
        data, size.width, size.height, 8, size.width * 4, colorSpace,
        kCGImageAlphaPremultipliedFirst);

    CGColorSpaceRelease(colorSpace);

    if (context == NULL)
    {
        NSLog(@"Could not create context");
        return nil;
    }

    CGImageRef ref = CGBitmapContextCreateImage(context);
    if (ref == NULL)
    {
        NSLog(@"Could not create image");
        return nil;
    }

    CGContextRelease(context);

    UIImage* image = [UIImage imageWithCGImage:ref];
    CFRelease(ref);

    return image;   
}

Here is a way to do it. First you allocate memory that you fill up with your RGB values, then you call the 'imageWithBytes' function.

The memory you allocate should be 4*width*height bytes. Every pixel is described by four bytes, in the order A,R,G,B.

Don't forget to free the memory when you're done!

+ (UIImage*)imageWithBytes:(unsigned char*)data size:(CGSize)size
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    if (colorSpace == NULL)
    {
        NSLog(@"Could not create color space");
        return nil;
    }

    CGContextRef context = CGBitmapContextCreate(
        data, size.width, size.height, 8, size.width * 4, colorSpace,
        kCGImageAlphaPremultipliedFirst);

    CGColorSpaceRelease(colorSpace);

    if (context == NULL)
    {
        NSLog(@"Could not create context");
        return nil;
    }

    CGImageRef ref = CGBitmapContextCreateImage(context);
    if (ref == NULL)
    {
        NSLog(@"Could not create image");
        return nil;
    }

    CGContextRelease(context);

    UIImage* image = [UIImage imageWithCGImage:ref];
    CFRelease(ref);

    return image;   
}
星軌x 2024-11-07 20:52:59

UIImage 扩展:

extension UIImage {

    class func imageWithColor(color: UIColor, size: CGSize) -> UIImage {

        let rect = CGRect(origin: CGPoint.zeroPoint, size: size)

        UIGraphicsBeginImageContext(rect.size)

        let context = UIGraphicsGetCurrentContext()

        CGContextSetFillColorWithColor(context, color.CGColor)
        CGContextFillRect(context, rect)

        let image = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        return image

    }

}

UIImage extension:

extension UIImage {

    class func imageWithColor(color: UIColor, size: CGSize) -> UIImage {

        let rect = CGRect(origin: CGPoint.zeroPoint, size: size)

        UIGraphicsBeginImageContext(rect.size)

        let context = UIGraphicsGetCurrentContext()

        CGContextSetFillColorWithColor(context, color.CGColor)
        CGContextFillRect(context, rect)

        let image = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        return image

    }

}
囚我心虐我身 2024-11-07 20:52:59
UIImageView *imageView = [[UIImageView alloc] initWithFrame:yourFrame];
imageView.backgroundColor = [UIColor colorWithRed:redValue green:greenValue blue:blueValue alpha:alphaValue];
[self.view addSubview:imageView];
[imageView release];

希望这有帮助

UIImageView *imageView = [[UIImageView alloc] initWithFrame:yourFrame];
imageView.backgroundColor = [UIColor colorWithRed:redValue green:greenValue blue:blueValue alpha:alphaValue];
[self.view addSubview:imageView];
[imageView release];

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文