2009-06-13 35 views
110

我想基於UIColor動態創建1x1 UIImage。如何動態在iPhone上創建一個彩色的1x1 UIImage?

我懷疑這可以很快用Quartz2d完成,並且我正在仔細研究試圖掌握基礎知識的文檔。然而,它看起來像有很多潛在的缺陷:沒有正確識別每個事物的位數和字節數,沒有指定正確的標誌,沒有釋放未使用的數據等。

這怎麼可以用Quartz 2d(或另一種更簡單的方法)?

回答

318

您可以使用CGContextSetFillColorWithColorCGContextFillRect此:

斯威夫特

extension UIImage { 
    class func image(with color: UIColor) -> UIImage { 
     let rect = CGRectMake(0.0, 0.0, 1.0, 1.0) 
     UIGraphicsBeginImageContext(rect.size) 
     let context = UIGraphicsGetCurrentContext() 

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

     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 

     return image 
    } 
} 

Swift3

extension UIImage { 
    class func image(with color: UIColor) -> UIImage { 
     let rect = CGRect(origin: CGPoint(x: 0, y:0), size: CGSize(width: 1, height: 1)) 
     UIGraphicsBeginImageContext(rect.size) 
     let context = UIGraphicsGetCurrentContext()! 

     context.setFillColor(color.cgColor) 
     context.fill(rect) 

     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 

     return image! 
    } 
} 

Objective-C的

+ (UIImage *)imageWithColor:(UIColor *)color { 
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); 
    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetFillColorWithColor(context, [color CGColor]); 
    CGContextFillRect(context, rect); 

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return image; 
} 
+19

不錯的一個:-)我建議把這個方法作爲一個類方法放到一個類中,然後它可以簡單地添加到一個項目中,並且可以像`[UIImage imageWithColor:[UIColor redColor]]一樣調用。 – 2012-04-08 12:19:20

-6

好吧,這不會是你想要的,但是這段代碼會畫出一條線。你可以調整它來表達一個觀點。或者至少從中得到一些信息。

使圖像1x1看起來有點奇怪。筆畫騎在線上,所以在0.5的筆畫寬度爲1.0應該工作。只是玩耍。

- (void)drawLine{ 

UIGraphicsBeginImageContext(CGSizeMake(320,300)); 

CGContextRef ctx = UIGraphicsGetCurrentContext(); 

float x = 0; 
float xEnd = 320; 
float y = 300; 

CGContextClearRect(ctx, CGRectMake(5, 45, 320, 300)); 

CGContextSetGrayStrokeColor(ctx, 1.0, 1.0); 

CGContextSetLineWidth(ctx, 1); 
CGPoint line[2] = { CGPointMake(x,y), CGPointMake(xEnd, y) }; 

CGContextStrokeLineSegments(ctx, line, 2); 

UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
} 
6

我用馬特·史蒂芬的回答很多次這樣做了它的類別:

@interface UIImage (mxcl) 
+ (UIImage *)squareImageWithColor:(UIColor *)color dimension:(int)dimension; 
@end 

@implementation UIImage (mxcl) 
+ (UIImage *)squareImageWithColor:(UIColor *)color dimension:(int)dimension { 
    CGRect rect = CGRectMake(0, 0, dimension, dimension); 
    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetFillColorWithColor(context, [color CGColor]); 
    CGContextFillRect(context, rect); 

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return image; 
} 
@end 
7

下面是根據馬特·斯蒂芬的代碼的另一種選擇。它會創建可調整大小的純色圖像,以便您可以重複使用它或更改其大小(例如,將其用於背景)。

+ (UIImage *)prefix_resizeableImageWithColor:(UIColor *)color { 
    CGRect rect = CGRectMake(0.0f, 0.0f, 3.0f, 3.0f); 
    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetFillColorWithColor(context, [color CGColor]); 
    CGContextFillRect(context, rect); 

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(1, 1, 1, 1)]; 

    return image; 
} 

將它放入UIImage類別並更改前綴。