Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于iPhone编程的CFDataRef和像素数据_Iphone_Objective C_Ios_Cgimageref_Cfdata - Fatal编程技术网

用于iPhone编程的CFDataRef和像素数据

用于iPhone编程的CFDataRef和像素数据,iphone,objective-c,ios,cgimageref,cfdata,Iphone,Objective C,Ios,Cgimageref,Cfdata,我试图访问图像的原始像素,但我一直遇到一个问题,让我真的卡住了。我对iPhone编程非常陌生,所以它可能非常简单 这是我的类,它使用来自图像的cgimagerf,并查看各个像素。这是我的密码 头文件: #import <QuartzCore/QuartzCore.h> #import <QuartzCore/QuartzCore.h> #import <CoreImage/CoreImage.h> @interface GraphingView : UIC

我试图访问图像的原始像素,但我一直遇到一个问题,让我真的卡住了。我对iPhone编程非常陌生,所以它可能非常简单

这是我的类,它使用来自图像的
cgimagerf
,并查看各个像素。这是我的密码

头文件:

#import <QuartzCore/QuartzCore.h>
#import <QuartzCore/QuartzCore.h>
#import <CoreImage/CoreImage.h>


@interface GraphingView : UIControl 
{
@private
    CAShapeLayer *shapeLayer;    
    CGImageRef pixelData;
}

@property(assign) CGImageRef pixelData;
@end
我的问题是:

函数
setPixelData
获取
CGImageRef
图像并将其复制到
pixelData
。然后,在
(void)updateGraph
方法中,我使用
CGImageRef pixelData
并创建一个
CFDataRef
来获取原始像素数据。然而,有点不对劲。因为当我使用
CFDataGetLength
方法使用
CFDataRef pixelRef
作为输入时,程序运行时会出现
EXC\u BAD\u ACCESS
错误。让我困惑的是,当我在
(void)setPixelData
函数中放入相同的两行代码(如上面的代码所示)并在
updateGraph
中注释掉代码时,程序运行得非常好

你知道是什么导致了这个错误,以及如何消除它吗

谢谢

山姆

啊。。。我找到了

它不起作用的原因是因为我有两个setter方法(其中一个没有在代码中显示)。那一个总是在
setPixelData
函数之前运行,因此从未设置过
PixelData CGImageRef
,因此,
CFDataRef
不存在,导致内存错误。

啊。。。我找到了


它不起作用的原因是因为我有两个setter方法(其中一个没有在代码中显示)。该函数总是在
setPixelData
函数之前运行,因此从未设置过
PixelData CGImageRef
,因此,
CFDataRef
不存在,导致内存错误。

为什么不使用
CGImageGetData()
?为什么不使用
CGImageGetData()
#import "GraphingView.h"
#import "iSpectrumViewController.h"

@implementation GraphingView

@synthesize pixelData;
- (void)awakeFromNib
{
    // set up a rounded border
    CALayer *layer = [self layer];

    // clear the view's background color so that our background
    // fits within the rounded border
    CGColorRef backgroundColor = [self.backgroundColor CGColor];
    self.backgroundColor = [UIColor clearColor];
    layer.backgroundColor = backgroundColor;


    shapeLayer = [CAShapeLayer layer];      

    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{

}

- (void)updateGraph
{
    // This is where my problem is:
     // Whenever the next line is executed the application stops working. It works fine in the setPixelData
     // down below.
    CFDataRef pixelRef = CGDataProviderCopyData(CGImageGetDataProvider(pixelData));
    CFIndex byteSize = CFDataGetLength(pixelRef);
    int intByteSize = (int) byteSize;
    NSLog(@"Number of Bytes contained %i",intByteSize);

     //Then get a pointer to the data so the information can be accessed
    const UInt8 *pixelPtr = CFDataGetBytePtr(pixelRef);


    // Do some drawing here using the data obtained from the image...

    [shapeLayer setPath:path];
    [shapeLayer setNeedsDisplay];
    CFRelease(path);    
}

-(void)setPixelData:(CGImageRef)pixelImage
{
     //This code takes the CGImage from my image and copies it to the pixelData CGImageRef defined in the header file


    //This gets the CFDataRef from the CGImage so the pixel information is available
    CFDataRef PixelCopy = CGDataProviderCopyData(CGImageGetDataProvider(pixelImage));
    CFIndex byteSize = CFDataGetLength(PixelCopy);
    int intByteSize = (int) byteSize;
    NSLog(@"Number of Bytes contained %i",intByteSize);

    pixelData = CGImageCreateCopy(pixelImage);
    [self updateGraph];

}
@end