how to upload and query color to parse iOS/objective c

172 Views Asked by At

In objective-c how do you upload and query a UIColor to Parse? I have been trying many particular things but with no outcome. I was able to convert a UIColor to NSData and save it to PFFile. But I have had no fortune in querying the file.

At this point I have no idea if I am doing anything right. Please help.

2

There are 2 best solutions below

1
On

Try this...it will work

PFObject *colorObj = [PFObject objectWithClassName:@"Colors"];
CIColor *color = [CIColor colorWithRed:155.0/255 green:172.0/255 blue:57.0/255 alpha:1.0];
colorObj[@"redColor"] = [NSNumber numberWithFloat:color.red];
colorObj[@"greenColor"] = [NSNumber numberWithFloat:color.green];
colorObj[@"blueColor"] = [NSNumber numberWithFloat:color.blue];
colorObj[@"forView"] = @"Label";
NSError *error;
BOOL isSaved = [colorObj save:&error];
if (isSaved) {
    //saved
} else {
    //error
}

PFQuery *query = [PFQuery queryWithClassName:@"Colors"];
NSArray *arr = [query findObjects];
NSInteger index = 0;
while (index < arr.count) {

    //getting view type to assign color
    NSString *viewType = [arr objectAtIndex:index][@"forView"];
    if ([viewType isEqualToString:@"Label"]) {

        //geeting color
        CGFloat red = [arr[index][@"redColor"] floatValue];
        CGFloat green = [arr[index][@"greenColor"] floatValue];
        CGFloat blue = [arr[index][@"blueColor"] floatValue];
        self.myLabel.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
    }
    index++;
}
0
On

In my opinion, you should convert colour to hex colour code. Then you can save hex colour codes to Parse or PFFile. You can query hex colour code from Parse or PFFile.

Try it with this way!