用于iPhone编程的CFDataRef和像素数据
我试图访问图像的原始像素,但我一直遇到一个问题,让我真的卡住了。我对iPhone编程非常陌生,所以它可能非常简单 这是我的类,它使用来自图像的用于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
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