2014-06-12 25 views
0

我試圖實現一個簡單的顏色選擇器組成的圖像在UIImageView。當用戶在手指上拖動手指時,我想讓相鄰視圖以當前像素顏色持續更新。顏色選擇器的窘境

我發現了什麼看起來像this question on SO東西接近@ rdelmar的回答了理想的解決方案,所以我借的代碼,並修改它的pickedColor發回的AddCategoryViewController的方法。但是,此方法(setColorview)未更新colorView

我的斷點和NSLog顯示顏色信息是從觸摸收集的,發送和接收的,但顯然沒有應用,如受影響的UIView colorView不反映顏色的事實所示。最明顯的問題是,物業colorView讀取null,我不明白爲什麼。

作爲一個獎勵,我真的很喜歡挑剔響應滑動的手指,而不僅僅是一個水龍頭。

我已經包括以下所有相關的代碼,具有重要的線標有// *******************

爲尋找謝謝!所有幫助讚賞!

代碼從這裏開始,文件被---------------------------分離:

// 
// UIView+UIView_ColorOfPoint.m 
// WMDGx 
// 
// 

#import "UIView+UIView_ColorOfPoint.h" 
#import <QuartzCore/QuartzCore.h> 


@implementation UIView (UIView_ColorOfPoint) 

-(UIColor *) colorOfPoint:(CGPoint)point 
{ 
    unsigned char pixel[4] = {0}; 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef context = CGBitmapContextCreate(pixel, 
               1, 1, 8, 4, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast); 

    CGContextTranslateCTM(context, -point.x, -point.y); 

    [self.layer renderInContext:context]; 

    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace); 
    UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 
            green:pixel[1]/255.0 blue:pixel[2]/255.0 
            alpha:pixel[3]/255.0]; 
    return color; 
} 

@end 




----------------------------------------- 


// 
// ColorPickerView.m 
// WMDGx 
// 
// 

#import "ColorPickerView.h" 

AddCategoryViewController *addCatVC; 

@implementation ColorPickerView 


- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint loc = [touch locationInView:self]; 
    self.pickedColor = [self colorOfPoint:loc]; 
    NSLog(@"Touches began"); 

    //******************** For sending color info to setColorView in AddCategoryViewController 
    addCatVC = [[AddCategoryViewController alloc]init]; 
    addCatVC.colorForColorView = self.pickedColor; 

    NSLog(@"Picked color is %@",self.pickedColor); 

    //******************** Call the method in AddCategoryViewController, which should display the color of the current pixel 
    [addCatVC setColorview]; 
} 


@end 



-------------------------------- 


// 
// AddCategoryViewController.m 
// WMDGx 
// 
// 

#import "AddCategoryViewController.h" 

@interface AddCategoryViewController() 

@end 

@implementation AddCategoryViewController 

NSMutableArray *array; 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) 
    { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Do any additional setup after loading the view. 
    self.catTextField.delegate = self; 
    [self.pickerImageView setUserInteractionEnabled:YES]; 

    self.colorView.layer.cornerRadius = 6; 

    // ********************I put this NSLog here to check the status of the property self.colorView 

    NSLog(@"Color view is %@",self.colorView);//_colorView UIView * 0x8a63d30 0x08a63d30 (from Variables View) 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [self.catTextField resignFirstResponder]; 
    return YES; 
} 


-(void)setColorview // ********************This is where the backgroundColor of self.colorView should be set 
{ 
    NSLog(@"colorForColorView is %@",self.colorForColorView); 

    NSLog(@"Color view is %@",self.colorView); //_colorView UIView * nil 0x00000000 (from Variables View) 

    [self.colorView setBackgroundColor:self.colorForColorView]; //colorView should display the color, but doesn't 

    NSLog(@"Color view is %@",self.colorView); //_colorView UIView * nil 0x00000000 (from Variables View) 

    NSLog(@"Color view color is %@",self.colorView.backgroundColor); 
} 

- (IBAction)saveButton:(UIBarButtonItem *)sender 
{ 
    if (self.catTextField.text.length < 1) 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No category created" 
                 message:@"Please create a new category" 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 
    } 

    else if (!self.colorView.backgroundColor) 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No color selected" 
                 message:@"Please select a color for your new category" 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 
    } 
    else 
    { 
     NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread]; 
     self.thisCategory = [WMDGCategory MR_createInContext:localContext]; 
     self.thisCategory.name = [self.catTextField.text uppercaseString]; 
     self.thisCategory.color = self.colorView.backgroundColor; 
     [localContext MR_saveToPersistentStoreAndWait]; 
     [self.thisCell setUserInteractionEnabled:NO]; 
     [self.delegate addCatControllerDidSave]; 
    } 
} 

- (IBAction)cancelButton:(UIBarButtonItem *)sender 
{ 
    [self.delegate addCatControllerDidCancel:self.thisCategory]; 

} 

@end 

回答

1

這裏是我的我爲此使用的代碼。除了觸摸開始,我對移動的觸摸具有相同的代碼,並且允許您還想要的滑動效果。我的相鄰視圖有一個200的標籤,我根據所選的顏色查找並設置。它看起來像你的鏈接代碼比我的更乾淨,但也許看到這將是有用的。

首先,當我接觸:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSSet *allTouches = [event allTouches]; 

    if ([allTouches count] != 1) 
     return; 

    UIView *picker = [self.view viewWithTag:100]; 

    UITouch *touch = [[allTouches allObjects] objectAtIndex:0]; 
    CGPoint p = [touch locationInView:self.view]; 
    if (CGRectContainsPoint(picker.frame, p)) 
    { 
     printf("Hit gradient!\n"); 
     p = [touch locationInView:picker]; 
     UIColor *c = [self getPixelColor:[UIImage imageNamed:@"gradient.png"] 
           xLoc:p.x 
           yLoc:p.y]; 

     UIView *display = [self.view viewWithTag:200]; // representative color 
     [display setBackgroundColor:c]; 
    } 
} 

用於獲取顏色(我相信這個代碼是改編自其它代碼在SO)代碼:

- (UIColor*)getPixelColor:(UIImage *)image xLoc:(int)x yLoc:(int)y 
{ 
    CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage)); 
    const UInt8* data = CFDataGetBytePtr(pixelData); 
    if (x < 0 || x >= image.size.width || y < 0 || y >= image.size.height) 
    { 
     CFRelease(pixelData); 
     return [UIColor whiteColor]; 
    } 
    int pixelInfo = ((image.size.width * y) + x) * 4; 

    UInt8 red = data[pixelInfo]; 
    UInt8 green = data[(pixelInfo + 1)]; 
    UInt8 blue = data[pixelInfo + 2]; 
    UInt8 alpha = data[pixelInfo + 3]; 
    CFRelease(pixelData); 

    UIColor* color = [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/255.0f]; 

    return color; 
} 
+0

酷!兩個問題:1)你在哪裏放兩個代碼塊? 2)這是否消除了我所包含的UIView類別的需求? – rattletrap99

+0

我有一個ColorPicker UIViewController,這個代碼在ColorPicker類中。這確實消除了對類別的需求,因爲我直接查詢了我正在使用的圖像,而不是渲染視圖然後查詢該像素。 (再次查看代碼有點有趣,因爲我沒有從UIImageView中獲取圖像,我直接從文件中查詢它。理論上,您可以直接在UIImageView('picker')中查詢圖像,但我沒有沒有經過測試。) –

+0

謝謝!我會在早上把這個旋轉一下,讓你知道發生了什麼。我仍然對這種空洞的看法感到沮喪。使用標籤時結果相同。 – rattletrap99