2009-06-13 30 views
110

Tôi muốn tạo một UIImage 1x1 động dựa trên UIColor.Cách tạo một UIImage 1x1 màu trên iPhone động?

Tôi nghi ngờ điều này có thể nhanh chóng được thực hiện với Quartz2d và tôi đang tìm kiếm tài liệu cố gắng nắm bắt các nguyên tắc cơ bản. Tuy nhiên, có vẻ như có rất nhiều cạm bẫy tiềm năng: không xác định số bit và byte cho mỗi thứ chính xác, không chỉ định đúng cờ, không giải phóng dữ liệu chưa sử dụng, v.v.

Làm thế nào để thực hiện điều này một cách an toàn 2d (hoặc cách đơn giản hơn)?

Trả lời

318

Bạn có thể sử dụng CGContextSetFillColorWithColorCGContextFillRect cho việc này:

Swift

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

Đẹp nhất :-) Tôi khuyên bạn nên đưa phương thức này vào một thể loại như một phương thức lớp, sau đó nó có thể được thêm vào một dự án đơn giản và được gọi bằng một dòng như '[UIImage imageWithColor: [UIColor redColor]]'. –

+7

Trong trường hợp bạn đang tạo những hình ảnh này trong vòng lặp hoặc sử dụng kích thước lớn hơn, tối ưu hóa sẽ chỉ sử dụng canvas mờ nếu màu có alpha. Như thế này: 'const CGFloat alpha = CGColorGetAlpha (color.CGColor); const BOOL opaque = alpha == 1; UIGraphicsBeginImageContextWithOptions (kích thước, opaque, 0); ' – hpique

+0

vui lòng cung cấp phiên bản swift – fnc12

-6

Ok, đây không phải chính xác những gì bạn muốn, nhưng mã này sẽ vẽ một dòng. Bạn có thể điều chỉnh nó để tạo ra một điểm. Hoặc ít nhất có được một ít thông tin từ nó.

Làm cho hình ảnh 1x1 có vẻ hơi lạ. Đột quỵ đi xe dòng, do đó, một nét của chiều rộng 1.0 0,5 nên làm việc. Chỉ cần chơi xung quanh.

- (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

Tôi đã từng trả lời nhiều lần Matt Steven nên làm một thể loại cho nó:

@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

Đây là một lựa chọn dựa trên mã Matt Stephen. Nó tạo ra một hình ảnh màu có thể thay đổi kích thước để bạn có thể sử dụng lại hoặc thay đổi kích thước của nó (ví dụ: sử dụng nó cho nền).

+ (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; 
} 

Đặt trong danh mục UIImage và thay đổi tiền tố.

Các vấn đề liên quan