Voici comment faire il
CGContextRef ctx;
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
//GET PIXEL FROM POINT
int index = 4*((width*round(yCoor))+round(xCoor));
int R = rawData[index];
int G = rawData[index+1];
int B = rawData[index+2];
NSLog(@"%d %d %d", R, G, B);
//IF YOU WANT TO ALTER THE PIXELS
int byteIndex = 0;
for(int ii = 0 ; ii < width * height ; ++ii)
{
rawData[byteIndex] = (char)(newPixelValue);
rawData[byteIndex+1] = (char)(newPixelValue);
rawData[byteIndex+2] = (char)(newPixelValue);
byteIndex += 4;
}
ctx = CGBitmapContextCreate(rawData,
CGImageGetWidth(imageRef),
CGImageGetHeight(imageRef),
8,
CGImageGetBytesPerRow(imageRef),
CGImageGetColorSpace(imageRef),
kCGImageAlphaPremultipliedLast);
imageRef = CGBitmapContextCreateImage(ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
CGContextRelease(ctx);
image = rawImage;
free(rawData);
Laide mais devrait fonctionner –
@WilliamJockusch J'ai implémenté ceci mais je veux détecter la couleur (de seulement 1 pixel sur l'écran) régulièrement (toutes les 0.04 secondes en utilisant une minuterie) créant une capture d'écran de tout l'écran lag mon application ... vous n'avez pas trouvé une façon plus efficace de mémoire de le faire avez-vous? Et des excuses pour avoir commenté un tel vieux fil! ;) – simonthumper
@simonthumper Malheureusement non. Si vous n'avez pas besoin de saisir l'écran, vous pouvez probablement accélérer les choses. (Cela dépend vraiment de la source des données d'image.) Cela dit, je serais tenté de créer une nouvelle question (bien que je mentionne que vous avez lu/absorbé celui-ci) si le problème que vous essayez de résoudre est suffisamment différent. –