Я пытаюсь получить доступ к необработанным пикселям изображения, но продолжаю сталкиваться с проблемой, которая действительно застряла. Я довольно новичок в программировании для iPhone, так что это может быть что-то действительно простое ...
Вот класс, который у меня есть, он использует CGImageRef
из изображения и смотрит на отдельные пиксели. Вот мой код
Заголовочный файл:
#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
И мой файл реализации:
#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
У меня проблема:
Функция setPixelData
получает изображение CGImageRef
и копирует его в pixelData
. Затем в методе (void) updateGraph
я использую CGImageRef pixelData
и создаю CFDataRef
для получения необработанных данных пикселей. Однако что-то не так. Потому что, когда я использую метод CFDataGetLength
, используя CFDataRef pixelRef
в качестве входных данных, я получаю ошибку EXC_BAD_ACCESS
при запуске программы. Что меня смущает, так это то, что когда я помещаю те же две строки кода в функцию (void) setPixelData
(как показано в приведенном выше коде) и закомментирую код в updateGraph
, тогда программа работает отлично.
Есть идеи, что может быть причиной этой ошибки и как ее устранить?
Спасибо
Sam