내 작업은 UIImage 객체에서 모든 이미지 픽셀을 가져 와서 변수에 저장하는 것입니다. 나 컬러 이미지를 그렇게하는 것은 어려운 일이 아니다 :UIImage 객체에서 모든 이미지 픽셀을 얻는 방법
CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
size_t ele = CGColorSpaceGetNumberOfComponents(colorSpace);
CGFloat cols = image.size.width;
CGFloat rows = image.size.height;
// Create memory for the input image
unsigned char *img_mem;
img_mem = (unsigned char*) malloc(rows*cols*4);
unsigned char *my_img;
my_img = (unsigned char *)malloc(rows*cols*3);
CGContextRef contextRef = CGBitmapContextCreate(img_mem, cols, // Width of bitmap
rows, // Height of bitmap
8, // Bits per component
cols*4, // Bytes per row
colorSpace, // Colorspace
kCGImageAlphaNoneSkipLast|kCGBitmapByteOrderDefault); // Bitmap info flags
CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpace);
unsigned int pos_new;
unsigned int pos_old;
for(int i=0; i<rows; i++)
{
pos_new = i*cols*3;
pos_old = i*cols*4;
for(int j=0; j<cols; j++)
{
my_img[j*3+pos_new] = img_mem[pos_old+j*4];
my_img[j*3+pos_new+1] = img_mem[pos_old+j*4+1];
my_img[j*3+pos_new+2] = img_mem[pos_old+j*4+2];
}
}
free(img_mem);
//All the pixels are installed in my_img
free(my_img);
내 문제는 위의 코드는 컬러 이미지에 유용하지만, 그레이 스케일 이미지 나는 그것을 할 수없는 방법에 대해 설명합니다. 어떤 아이디어?
동적으로 처리해야하는 경우에 대한 예를들시겠습니까? 감사. – Unheilig