UIColor and CIColor how do they compare and what is the purpose of having two of them?

前端 未结 4 1215
醉梦人生
醉梦人生 2021-01-23 02:35

Recently I encountered some code where trying to get the CIColor property of a UIColor fails. The UIColor was initialized as a simple [UIColor blackColor] and trying to get the

相关标签:
4条回答
  • 2021-01-23 02:41

    The doc says

    Initializes a Core Image color object using an AppKit color object.

    So you need create CIColor from NSColor class

    CIColor *aCIColor = [[CIColor alloc] initWithColor:[NSColor blackColor]];
    

    The diff between UIColor and CIColor in that UIColor from UIKit framework and using in iOS. From Doc Available in iOS 2.0 and later

    A UIColor object represents color and sometimes opacity (alpha value). You can use UIColor objects to store color data, and during drawing you can use them to set the current fill and stroke colors

    CIColor from CoreImage framework and using in Mac OSX Available in OS X v10.4 and later

    The CIColor class contains color values and the color space for which the color values are valid. You use CIColor objects in conjunction with other Core Image classes, such as CIFilter, CIContext,and CIImage, to take advantage of the built-in Core Image filters when processing images.

    0 讨论(0)
  • 2021-01-23 02:48

    The documentation for the CIColor property on UIImage says:

    This property throws an exception if the color object was not initialized with a Core Image color.

    So if you want to access a UIColor's CIColor, it needs to have been created from a CIColor in the first place.

    On the other hand, you can make a new CIColor from any UIColor using the initWithColor: initializer:

    CIColor *color = [[CIColor alloc] initWithColor:UIColor.blackColor];
    
    0 讨论(0)
  • 2021-01-23 02:50

    try this(swift version)

     var clcor = CIColor(color: UIColor.redColor())
    

    obj :

    CIColor *aCIColor = [[CIColor alloc] initWithColor:[NSColor blackColor]];
    
    0 讨论(0)
  • 2021-01-23 03:01

    CIColor, like UIColor, is used to represent channel dimensions of a color.

    The difference as the documentation puts it is:

    You use CIColor objects in conjunction with other Core Image classes, such as CIFilter, CIContext,and CIImage, to take advantage of the built-in Core Image filters when processing images.

    From WWDC 2013:

    So, because, you know, and CIColor doesn't handle color spaces, it won't do CMYK color, it won't do patterns.

    So, these are all things that you're going to lose.

    If you ever think that you're going to need that, then probably not the right strategy.

    An alternate way, other than what has been already answered before me, You could have created the CIColor by creating CGColor of Black UIColor like:

    CIColor *color = [CIColor colorWithCGColor:[[UIColor blackColor] CGColor]];
    
    0 讨论(0)
提交回复
热议问题