2013-03-17 48 views
0

我嘗試的東西來創建一個框架和顏色的UIImage的,但不知道怎樣纔是正確的方式。如何自動繪製邊框和顏色的UIImage?

UIImage *aImage = [UIImage imageWithCIImage:[CIImage imageWithColor:[CIColor colorWithCGColor:[UIColor redColor].CGColor]]]; 

是上述線路是否正確?如何創建圖像大小?請幫忙!

回答

7

做出UIImage類別。

UIImage(Color)

+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size 
{ 
    UIGraphicsBeginImageContextWithOptions(size, YES, 0); 

    [color set]; 
    UIBezierPath * path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, size.width, size.height)]; 
    [path fill]; 

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

    return image; 
} 

使用方法:

#import "UIImage + Color" 

UIImage *image = [UIImage imageWithColor:[UIColor redColor] andSize:yourSize];