这可能不是您最初想到的。我知道如何使用UIImage,但现在我需要知道如何使用以下内容创建一个“空白”UIImage:
CGRect screenRect = [self.view bounds];
好吧,就是那些尺寸。无论如何,我想知道如何创建一个以这些尺寸为基础、全白色的UIImage。这里没有实际的图片。
这是可能的吗?我确信是可能的,但也许我错了。
编辑
这需要是“白色”的图像,而不是“空白”的。 :)
这可能不是您最初想到的。我知道如何使用UIImage,但现在我需要知道如何使用以下内容创建一个“空白”UIImage:
CGRect screenRect = [self.view bounds];
好吧,就是那些尺寸。无论如何,我想知道如何创建一个以这些尺寸为基础、全白色的UIImage。这里没有实际的图片。
这是可能的吗?我确信是可能的,但也许我错了。
这需要是“白色”的图像,而不是“空白”的。 :)
您需要使用CoreGraphics
,方法如下。
CGSize size = CGSizeMake(desiredWidth, desiredHeight);
UIGraphicsBeginImageContextWithOptions(size, YES, 0);
[[UIColor whiteColor] setFill];
UIRectFill(CGRectMake(0, 0, size.width, size.height));
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
代码通过传递参数——大小、不透明度和缩放,创建一个新的CoreGraphics
图像上下文。通过将缩放设置为0
,iOS会自动选择适合当前设备的值。[UIColor whiteColor]
。紧接着,画布就用UIRectFill()
实际填充了这种颜色,并传递了一个填充画布的矩形。UIImage
被创建出当前上下文,并关闭了上下文。因此,图像变量包含了所需大小的白色填充的UIImage
。Swift 版本:
extension UIImage {
static func emptyImage(with size: CGSize) -> UIImage? {
UIGraphicsBeginImageContext(size)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
UIImage?
。 - AamirR UIGraphicsBeginImageContext(CGSizeMake(width, height));
CGContextAddRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height)); // this may not be necessary
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
以上代码假设您绘制的图像大小为宽x高。它将矩形添加到图形上下文中,但这可能并非必要。请自行尝试。这是正确的方法。 :)
或者,如果您想创建当前视图的快照,您需要输入以下代码;
UIGraphicsBeginImageContext(CGSizeMake(self.view.size.width, self.view.size.height));
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
如果您使用图层,请不要忘记包含Quartz库。
let size = CGSize(width: 32.0, height: 32.0)
if var image = UIImage.imageWithSize(size:size, UIColor.clear) {
//image was successfully created, do additional stuff with it here.
}
以下是针对Swift 3.x的内容:
extension UIImage {
static func imageWithSize(size : CGSize, color : UIColor = UIColor.white) -> UIImage? {
var image:UIImage? = nil
UIGraphicsBeginImageContext(size)
if let context = UIGraphicsGetCurrentContext() {
context.setFillColor(color.cgColor)
context.addRect(CGRect(origin: CGPoint.zero, size: size));
context.drawPath(using: .fill)
image = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext()
return image
}
}
static func imageWithSize(size : CGSize, color : UIColor) -> UIImage {
UIGraphicsBeginImageContext(size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextAddRect(context, CGRect(x: 0, y: 0, width: size.width, height: size.height));
CGContextDrawPath(context, .Fill)
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image
}
针对Xamarin.iOS的C#相同解决方案:
UIGraphics.BeginImageContext(new CGSize(width, height));
var image = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
view.backgroundColor = [UIColor whiteColor];
。 - nielsbot