I used this way
- (void) imagePixel:(UIImage *)image
{
struct pixel {
unsigned char r, g, b, a;
};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGImageRef imageRef = image.CGImage;
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
struct pixel *pixels = (struct pixel *) calloc(1, sizeof(struct pixel) * width * height);
size_t bytesPerComponent = 8;
size_t bytesPerRow = width * sizeof(struct pixel);
CGContextRef context = CGBitmapContextCreate(pixels, width, height, bytesPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
unsigned long numberOfPixels = width * height;
if (context != NULL) {
for (unsigned i = 0; i < numberOfPixels; i++) {
//you can add code here
pixels[i].r;
pixels[i].g;
pixels[i].b;
}
free(pixels);
CGContextRelease(context);
}
CGColorSpaceRelease(colorSpace);
}
Update:
Yes, you can recreating image before release context and free pixel by
CGImageRef imageR = CGBitmapContextCreateImage(context);
UIImage *outputImage = [UIImage imageWithCGImage:imageR];
at the end you should release imageR with
CGImageRelease(imageR)