我有以下代码试图测试像素矩形中的每个像素
px, py = 触摸位置
dx, dy = 要测试的矩形的大小
UIImage *myImage; // image from which pixels are read
int ux = px + dx;
int uy = py + dy;
for (int x = (px-dx); x <= ux; ++x)
{
for (int y = (py-dy); y <= uy; ++y)
{
unsigned char pixelData[] = { 0, 0, 0, 0 };
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixelData, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGImageRef cgimage = myImage.CGImage;
int imageWidth = CGImageGetWidth(cgimage);
int imageHeight = CGImageGetHeight(cgimage);
CGContextDrawImage(context, CGRectMake(-px, py - imageHeight, imageWidth, imageHeight), cgimage);
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
// Here I have latest pixel, with RBG values stored in pixelData that I can test
}
}
内部循环中的代码正在抓取位置 x, y 处的像素。有没有更有效的方法从 UIImage (myImage) 中从 (x-dx,y-dy)、(x+dx,y+dy) 中获取整个像素矩形?