iPhone SDK:从UIImageViewClass调用方法到ViewController

iPhone SDK:从UIImageViewClass调用方法到ViewController,iphone,ios,xcode,uiviewcontroller,uiimageview,Iphone,Ios,Xcode,Uiviewcontroller,Uiimageview,在.m ImageView类文件中有此函数 - (UIColor*) getPixelColorAtLocation:(CGPoint)point { 如何从视图控制器调用此函数?我试过了` - (void)viewDidLoad { CGPoint point = CGPointMake(100, 100); UIColor* color = [ImageView getPixelColorAtLocation:point]; }` 但这给了我一个警告:“扇区‘ge

在.m ImageView类文件中有此函数

- (UIColor*) getPixelColorAtLocation:(CGPoint)point {
如何从视图控制器调用此函数?我试过了`

    - (void)viewDidLoad
{
    CGPoint point = CGPointMake(100, 100);
    UIColor* color = [ImageView getPixelColorAtLocation:point];
}`
但这给了我一个警告:“扇区‘getPixelColorAtLocation:’没有已知的类方法(我确实将ImageView类文件导入了view控制器)

ImageView中的My.h文件

    #import <Foundation/Foundation.h>
@interface ImageView : UIImageView {
UIColor* lastColor;
//id pickedColorDelegate;
}

@property (nonatomic, retain) UIColor* lastColor;
//@property (nonatomic, retain) id pickedColorDelegate;


- (UIColor*) getPixelColorAtLocation:(CGPoint)point;
- (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef)inImage;
@end
    #import "ImageView.h"
#import "ViewController.h"
#import <CoreGraphics/CoreGraphics.h>
#import <QuartzCore/CoreAnimation.h>
@implementation ImageView

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

- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
    if (self.hidden==YES) {
        //color wheel is hidden, so don't handle  this as a color wheel event.
        [[self nextResponder] touchesEnded:touches withEvent:event];
        return;
    }

    UITouch* touch = [touches anyObject];
   // ColorPickerAppDelegate *mainDelegate = (ColorPickerAppDelegate *)[[UIApplication sharedApplication] delegate];
    CGPoint point = [touch locationInView:self]; //where image was tapped

    self.lastColor = [self getPixelColorAtLocation:point];
    NSLog(@"color %@",lastColor);
    //[pickedColorDelegate pickedColor:(UIColor*)self.lastColor];
}


- (UIColor*) getPixelColorAtLocation:(CGPoint)point {
    UIColor* color = nil;
    CGImageRef inImage = self.image.CGImage;
    // Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue
    CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
    if (cgctx == NULL) { return nil; /* error */ }

    size_t w = CGImageGetWidth(inImage);
    size_t h = CGImageGetHeight(inImage);
    CGRect rect = {{0,0},{w,h}};

    // Draw the image to the bitmap context. Once we draw, the memory
    // allocated for the context for rendering will then contain the
    // raw image data in the specified color space.
    CGContextDrawImage(cgctx, rect, inImage);

    // Now we can get a pointer to the image data associated with the bitmap
    // context.
    unsigned char* data = CGBitmapContextGetData (cgctx);
    if (data != NULL) {
        //offset locates the pixel in the data from x,y.
        //4 for 4 bytes of data per pixel, w is width of one row of data.
        int offset = 4*((w*round(point.y))+round(point.x));
        int alpha =  data[offset];
        int red = data[offset+1];
        int green = data[offset+2];
        int blue = data[offset+3];
        NSLog(@"offset: %i colors: RGB A %i %i %i  %i",offset,red,green,blue,alpha);
        color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];
    }

    // When finished, release the context
    CGContextRelease(cgctx);
    // Free image data memory for the context
    if (data) { free(data); }

    return color;
}



- (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage {

    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;

    // Get image width, height. We'll use the entire image.
    size_t pixelsWide = CGImageGetWidth(inImage);
    size_t pixelsHigh = CGImageGetHeight(inImage);

    // Declare the number of bytes per row. Each pixel in the bitmap in this
    // example is represented by 4 bytes; 8 bits each of red, green, blue, and
    // alpha.
    bitmapBytesPerRow   = (pixelsWide * 4);
    bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);

    // Use the generic RGB color space.
    colorSpace = CGColorSpaceCreateDeviceRGB();

    if (colorSpace == NULL)
    {
        fprintf(stderr, "Error allocating color space\n");
        return NULL;
    }

    // Allocate memory for image data. This is the destination in memory
    // where any drawing to the bitmap context will be rendered.
    bitmapData = malloc( bitmapByteCount );
    if (bitmapData == NULL)
    {
        fprintf (stderr, "Memory not allocated!");
        CGColorSpaceRelease( colorSpace );
        return NULL;
    }

    // Create the bitmap context. We want pre-multiplied ARGB, 8-bits
    // per component. Regardless of what the source image format is
    // (CMYK, Grayscale, and so on) it will be converted over to the format
    // specified here by CGBitmapContextCreate.
    context = CGBitmapContextCreate (bitmapData,
                                     pixelsWide,
                                     pixelsHigh,
                                     8,      // bits per component
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedFirst);
    if (context == NULL)
    {
        free (bitmapData);
        fprintf (stderr, "Context not created!");
    }

    // Make sure and release colorspace before returning
    CGColorSpaceRelease( colorSpace );

    return context;
}
#导入
@接口ImageView:UIImageView{
UIColor*lastColor;
//id pickedColorDelegate;
}
@属性(非原子,保留)UIColor*lastColor;
//@属性(非原子,保留)id pickedColorDelegate;
-(UIColor*)获取像素颜色定位:(CGPoint)点;
-(CGContextRef)在图像中创建argbbitMapContextFromImage:(CGImageRef);
@结束
ImageView中的My.m文件

    #import <Foundation/Foundation.h>
@interface ImageView : UIImageView {
UIColor* lastColor;
//id pickedColorDelegate;
}

@property (nonatomic, retain) UIColor* lastColor;
//@property (nonatomic, retain) id pickedColorDelegate;


- (UIColor*) getPixelColorAtLocation:(CGPoint)point;
- (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef)inImage;
@end
    #import "ImageView.h"
#import "ViewController.h"
#import <CoreGraphics/CoreGraphics.h>
#import <QuartzCore/CoreAnimation.h>
@implementation ImageView

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

- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
    if (self.hidden==YES) {
        //color wheel is hidden, so don't handle  this as a color wheel event.
        [[self nextResponder] touchesEnded:touches withEvent:event];
        return;
    }

    UITouch* touch = [touches anyObject];
   // ColorPickerAppDelegate *mainDelegate = (ColorPickerAppDelegate *)[[UIApplication sharedApplication] delegate];
    CGPoint point = [touch locationInView:self]; //where image was tapped

    self.lastColor = [self getPixelColorAtLocation:point];
    NSLog(@"color %@",lastColor);
    //[pickedColorDelegate pickedColor:(UIColor*)self.lastColor];
}


- (UIColor*) getPixelColorAtLocation:(CGPoint)point {
    UIColor* color = nil;
    CGImageRef inImage = self.image.CGImage;
    // Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue
    CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
    if (cgctx == NULL) { return nil; /* error */ }

    size_t w = CGImageGetWidth(inImage);
    size_t h = CGImageGetHeight(inImage);
    CGRect rect = {{0,0},{w,h}};

    // Draw the image to the bitmap context. Once we draw, the memory
    // allocated for the context for rendering will then contain the
    // raw image data in the specified color space.
    CGContextDrawImage(cgctx, rect, inImage);

    // Now we can get a pointer to the image data associated with the bitmap
    // context.
    unsigned char* data = CGBitmapContextGetData (cgctx);
    if (data != NULL) {
        //offset locates the pixel in the data from x,y.
        //4 for 4 bytes of data per pixel, w is width of one row of data.
        int offset = 4*((w*round(point.y))+round(point.x));
        int alpha =  data[offset];
        int red = data[offset+1];
        int green = data[offset+2];
        int blue = data[offset+3];
        NSLog(@"offset: %i colors: RGB A %i %i %i  %i",offset,red,green,blue,alpha);
        color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];
    }

    // When finished, release the context
    CGContextRelease(cgctx);
    // Free image data memory for the context
    if (data) { free(data); }

    return color;
}



- (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage {

    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;

    // Get image width, height. We'll use the entire image.
    size_t pixelsWide = CGImageGetWidth(inImage);
    size_t pixelsHigh = CGImageGetHeight(inImage);

    // Declare the number of bytes per row. Each pixel in the bitmap in this
    // example is represented by 4 bytes; 8 bits each of red, green, blue, and
    // alpha.
    bitmapBytesPerRow   = (pixelsWide * 4);
    bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);

    // Use the generic RGB color space.
    colorSpace = CGColorSpaceCreateDeviceRGB();

    if (colorSpace == NULL)
    {
        fprintf(stderr, "Error allocating color space\n");
        return NULL;
    }

    // Allocate memory for image data. This is the destination in memory
    // where any drawing to the bitmap context will be rendered.
    bitmapData = malloc( bitmapByteCount );
    if (bitmapData == NULL)
    {
        fprintf (stderr, "Memory not allocated!");
        CGColorSpaceRelease( colorSpace );
        return NULL;
    }

    // Create the bitmap context. We want pre-multiplied ARGB, 8-bits
    // per component. Regardless of what the source image format is
    // (CMYK, Grayscale, and so on) it will be converted over to the format
    // specified here by CGBitmapContextCreate.
    context = CGBitmapContextCreate (bitmapData,
                                     pixelsWide,
                                     pixelsHigh,
                                     8,      // bits per component
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedFirst);
    if (context == NULL)
    {
        free (bitmapData);
        fprintf (stderr, "Context not created!");
    }

    // Make sure and release colorspace before returning
    CGColorSpaceRelease( colorSpace );

    return context;
}
#导入“ImageView.h”
#导入“ViewController.h”
#进口
#进口
@实现图像视图
@合成lastColor;
//@综合挑选颜色代表;
-(id)initWithFrame:(CGRect)帧
{
self=[super initWithFrame:frame];
如果(自我){
//初始化代码
}
回归自我;
}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event{
如果(self.hidden==是){
//颜色控制盘是隐藏的,因此不要将其作为颜色控制盘事件处理。
[[self nextResponder]touchesend:toucheevent:event];
返回;
}
UITouch*touch=[触摸任何对象];
//ColorPickerAppDelegate*mainDelegate=(ColorPickerAppDelegate*)[[UIApplication sharedApplication]委托];
CGPoint=[touch locationInView:self];//点击图像的位置
self.lastColor=[self-getPixelColorAtLocation:point];
NSLog(@“颜色%@”,lastColor);
//[pickedColorDelegate pickedColor:(UIColor*)self.lastColor];
}
-(UIColor*)获取像素颜色定位:(CGPoint)点{
UIColor*color=nil;
CGImageRef inImage=self.image.CGImage;
//创建屏幕外位图上下文以绘制图像。格式ARGB为每个像素4个字节:Alpa、红色、绿色、蓝色
CGContextRef cgctx=[self-createARGBBitmapContextFromImage:inImage];
如果(cgctx==NULL){返回nil;/*error*/}
尺寸w=CGImageGetWidth(InImages);
大小\u t h=CGImageGetHeight(InImages);
CGRect rect={{0,0},{w,h};
//将图像绘制到位图上下文。一旦绘制,内存
//然后,为渲染上下文分配的将包含
//指定颜色空间中的原始图像数据。
CGContextDrawImage(cgctx、rect、inImage);
//现在我们可以得到一个指向与位图关联的图像数据的指针
//上下文。
无符号字符*数据=CGBitmapContextGetData(cgctx);
如果(数据!=NULL){
//偏移量从x,y定位数据中的像素。
//4对于每像素4字节的数据,w是一行数据的宽度。
整数偏移=4*((w*圆(点y))+圆(点x));
int alpha=数据[偏移量];
int red=数据[偏移量+1];
int绿色=数据[偏移量+2];
蓝色整数=数据[偏移量+3];
NSLog(@“偏移量:%i颜色:RGB A%i%i%i”,偏移量,红色,绿色,蓝色,alpha);
颜色=[UIColor color WITHRED:(红色/255.0f)绿色:(绿色/255.0f)蓝色:(蓝色/255.0f)阿尔法(阿尔法/255.0f)];
}
//完成后,释放上下文
CGContextRelease(cgctx);
//为上下文释放图像数据内存
if(data){free(data);}
返回颜色;
}
-(CGContextRef)在图像中创建argbbitMapContextFromImage:(CGImageRef){
CGContextRef context=NULL;
CGCOLORSPACTEREF色彩空间;
void*位图数据;
int位映射字节计数;
int bitmapBytesPerRow;
//获取图像宽度和高度。我们将使用整个图像。
大小\u t像素宽度=CGImageGetWidth(inImage);
大小\u t像素H=CGImageGetHeight(inImage);
//声明每行的字节数。此对话框中位图中的每个像素
//示例由4个字节表示;红色、绿色、蓝色和蓝色各8位
//阿尔法。
bitmapBytesPerRow=(像素宽度*4);
bitmapByteCount=(bitmapBytesPerRow*pixelsHigh);
//使用通用RGB颜色空间。
colorSpace=CGColorSpaceCreateDeviceRGB();
if(colorSpace==NULL)
{
fprintf(stderr,“分配颜色空间时出错”);
返回NULL;
}
//为图像数据分配内存。这是内存中的目标
//将渲染位图上下文中的任何图形。
bitmapData=malloc(bitmapByteCount);
if(bitmapData==NULL)
{
fprintf(stderr,“内存未分配!”);
CGCOLORSPACTERELEASE(色彩空间);
返回NULL;
}
//创建位图上下文。我们需要预乘的ARGB,8位
//每个组件。无论源图像格式是什么
//(CMYK、灰度等)它将转换为
//此处由CGBitmapContextCreate指定。
上下文=CGBitmapContextCreate(bitmapData,
pixelsWide,
pixelsHigh,
8,//每个组件的位
bitmapBytesPerRow,
色彩空间,
KCGIMAGEAlphaPremultipledFirst);
if(上下文==NULL)
{
免费(位图数据);
fprintf(stderr,“未创建上下文!”);
}
//确保在返回之前释放颜色空间
CGCOLORSPACTERELEASE(色彩空间);
返回上下文;
}

您必须在实例中调用ImageView(
使用alloc]init]
) 如果你使用

- (UIColor*) getPixelColorAtLocation:(CGPoint)point 
或者让它静止

+ (UIColor*) getPixelColorAtLocation:(CGPoint)point

在你的
ImageView
类的
.h
文件中定义你的方法,我知道了!好吧,所以当我创建ImageView类时,它没有连接到我的目标,这就是为什么它没有接收到任何信号,也不允许我调用该类的方法。苹果又一次让你的生活痛苦了!不管怎样,是为了将类文件连接到目标,我遵循以下步骤: