2016-01-12 40 views

回答

0

您可以將您的UIColor轉換成組件,然後存儲它:

UIColor *orgColor = [UIColor blackColor]; 
const CGFloat *colorComponents = CGColorGetComponents(orgColor.CGColor);// RGBA 
NSInteger count = CGColorGetNumberOfComponents(orgColor.CGColor); 
CGFloat r, g, b, a; // save to db 
if (count == 2) { 
    r = g = b = colorComponents[0]; 
    a = colorComponents[1]; 
}else if (count == 4) { 
    r = colorComponents[0]; 
    g = colorComponents[1]; 
    b = colorComponents[2]; 
    a = colorComponents[3]; 
}else{ 
    NSLog(@"What is this?"); 
} 

,那麼你可以轉換RGBA回到你的UIColor對象:

UIColor *newColor = [[UIColor alloc] initWithRed:r green:g blue:b alpha:a]; 

如果有必要,美國可以CONV RGB到NSInteger的用CFF數據0xFF:

NSInteger colorInteger = ((NSInteger)(r*0xFF)<<(8*3)) 
         + ((NSInteger)(g*0xFF)<<(8*2)) 
         + ((NSInteger)(b*0xFF)<<(8*1)) 
         + ((NSInteger)(a*0xFF)<<(8*0)); 
0

最後我明白了。它完美的作品。

NSManagedObject *newDevice; 

- (NSManagedObjectContext *)managedObjectContext { 

    NSManagedObjectContext *context = nil; 

    id delegate = [[UIApplication sharedApplication] delegate]; 

    if ([delegate performSelector:@selector(managedObjectContext)]) 

{ 
     context = [delegate managedObjectContext]; 

    } 

    return context; 

} 


CGColorRef colorRef = [UIColor grayColor].CGColor; 

NSString *colorString = [CIColorcolorWithCGColor:colorRef].stringRepresentation; 

NSManagedObjectContext *context = [self managedObjectContext]; 
newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"ImageDetail" inManagedObjectContext:context]; 

NSData *imageData = UIImagePNGRepresentation(_imageView.image); 

[newDevice setValue:colorString forKey:@"color"];