下面是我复制的代码(从这个站点),由于原始代码无法编译,所以只做了一点修改。我想操纵字节数组进行边缘检测并最终对颜色进行简单的更改,但首先我想让基本代码正常工作。目前,系统编译运行。它在屏幕上显示一头画得很糟糕的大象。当我触摸图像时,它会消失。单步执行显示 imageWithData 的结果为 0x0。我用 png 和 bmp 都试过了,结果相同
关于我做错了什么的任何线索?!
ImageViewDrawable 定义为:
@interface ImageViewDrawable : UIImageView
// I am using the following code to initialize this ImageView
ImageViewDrawable * uiv = [[ImageViewDrawable alloc] initWithImage:[UIImage imageNamed:@"ele.png"] ];
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// get and work on pixel data
NSData* pixelData = (NSData*) CGDataProviderCopyData(CGImageGetDataProvider(self.image.CGImage));
char* bytes =[pixelData bytes];
// Take away the red pixel, assuming 32-bit RGBA
for(int i = 0; i < [pixelData length]; i += 4) {
bytes[i] = bytes[i]; // red
bytes[i+1] = bytes[i+1]; // green
bytes[i+2] = bytes[i+2]; // blue
bytes[i+3] = bytes[i+3]; // alpha
}
// convert pixel back to uiiimage
NSData* newPixelData = [NSData dataWithBytes:bytes length:[pixelData length]];
char * bytes2 =[pixelData bytes];
UIImage * newImage = [UIImage imageWithData:newPixelData] ;
[self setImage: newImage ];
}