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

| 对于阴影,我想制作一个1px x 2px的UIImage并手动将像素设置为某些RGBA值。 我怎样才能做到这一点?我宁愿不要用小png污染我的捆绑包。 谢谢!     
已邀请:
这是一种方法。首先,您分配用RGB值填充的内存,然后调用\'imageWithBytes \'函数。 您分配的内存应为4 *宽*高字节。每个像素由四个字节描述,顺序为A,R,G,B。 完成后,别忘了释放内存! +(UIImage *)imageWithBytes :(无符号字符*)数据大小:(CGSize)大小 {     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();     如果(colorSpace == NULL)     {         NSLog(@ \“无法创建色彩空间\”);         返回零;     }     CGContextRef context = CGBitmapContextCreate(         数据,size.width,size.height,8,size.width * 4,colorSpace,         kCGImageAlphaPremultipliedFirst);     CGColorSpaceRelease(colorSpace);     如果(上下文== NULL)     {         NSLog(@ \“无法创建上下文\”);         返回零;     }     CGImageRef ref = CGBitmapContextCreateImage(context);     如果(ref == NULL)     {         NSLog(@ \“无法创建图像\”);         返回零;     }     CGContextRelease(context);     UIImage *图片= [UIImage imageWithCGImage:ref];     CFRelease(参考);     返回图像; }     
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

    }

}
    
UIImageView *imageView = [[UIImageView alloc] initWithFrame:yourFrame];
imageView.backgroundColor = [UIColor colorWithRed:redValue green:greenValue blue:blueValue alpha:alphaValue];
[self.view addSubview:imageView];
[imageView release];
希望这可以帮助     

要回复问题请先登录注册