How to sort pixels in an UIImageView?
<p>I have a UIImageView and I want to analyze the image. The analyzer method most to detect the 5 most used pixels. I have two class where I implement the pixels:</p>

<p>The first class:</p>

<p>RGBAPixel.h</p>

<pre><code>@interface RGBAPixel : NSObject

   @property unsigned int reds;
   @property unsigned int greens;
   @property unsigned int blues;

@end
</code></pre>

<p>The second class:
Pixel.h</p>

<pre><code>@interface Pixel : NSObject

   @property (nonatomic, strong) RGBAPixel *rgbpixel;
   @property unsigned int key;

@end
</code></pre>

<p>RGBAPixels contains the pixels red, green, and blue components.</p>

<p>I use the key property to count how many times occurs the pixel in the image.</p>

<p>The analyze method :</p>

<pre><code>-(IBAction)analyzeImage:(id)sender {

    UIImage * inImage = imageView.image;
    CGImageRef rawImageRef = inImage.CGImage;

    CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(rawImageRef));
    const UInt8 *rawPixelData = CFDataGetBytePtr(data);

    NSUInteger imageHeight = CGImageGetHeight(rawImageRef);
    NSUInteger imageWidth  = CGImageGetWidth(rawImageRef);
    NSUInteger bytesPerRow = CGImageGetBytesPerRow(rawImageRef);
    NSUInteger stride = CGImageGetBitsPerPixel(rawImageRef) / 8;

    NSMutableArray *pixelsarray2 = [[NSMutableArray alloc] init];
    NSMutableArray *pixelsarray3 = [[NSMutableArray alloc] init];
    NSMutableArray *pixelsarray4 = [[NSMutableArray alloc] init];

    for (int row = 0; row &lt; imageHeight; row++) {
        const UInt8 *rowPtr = rawPixelData + bytesPerRow * row;
        for (int column = 0; column &lt; imageWidth; column++) {
            RGBAPixel *pixel = [[RGBAPixel alloc] init];
            pixel.reds = rowPtr[0];
            pixel.greens = rowPtr[1];
            pixel.blues = rowPtr[2];
            rowPtr += stride;
            Pixel *pix = [[Pixel alloc] init];
            pix.rgbpixel = pixel;
            pix.key = 1;
            [pixelsarray2 addObject:pixel];

            if (![pixelsarray3 containsObject:pixel]) {
             [pixelsarray2 addObject:pix];
             [pixelsarray3 addObject:pixel];

             }

             else {
             NSInteger index = [pixels3 indexOfObject:pixel];
             Pixel *helperPixel = [pixelsarray2 objectAtIndex:index];
             helperPixel.key ++;
             [pixelsarray4 addObject:helperPixel];
             helperPixel = nil;

             }
        }
    }

    NSArray *unsortedPixels = pixelsarray4;
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"key" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    NSArray *sortedPixels;
    sortedPixels = [unsortedPixels sortedArrayUsingDescriptors:sortDescriptors];

    Pixel *pix1 = [sortedPixels objectAtIndex:1];
    RGBAPixel *pixel1 = pix1.rgbpixel;
    NSLog(@"color1: %i %i %i:", pixel1.reds, pixel1.greens, pixel1.blues);
    UIColor *color1 = [[UIColor alloc]initWithRed:(pixel1.reds/255.0f) green:(pixel1.greens/255.0f) blue:(pixel1.blues/255.0f) alpha:1];

    Pixel *pix2 = [sortedPixels objectAtIndex:2];
    RGBAPixel *pixel2 = pix2.rgbpixel;
    NSLog(@"color2: %i %i %i:", pixel2.reds, pixel2.greens, pixel2.blues);
    UIColor *color2 = [[UIColor alloc]initWithRed:(pixel2.reds/255.0f) green:(pixel2.greens/255.0f) blue:(pixel2.blues/255.0f) alpha:1];

    Pixel *pix3 = [sortedPixels objectAtIndex:3];
    RGBAPixel *pixel3 = pix3.rgbpixel;
    NSLog(@"color3: %i %i %i:", pixel3.reds, pixel3.greens, pixel3.blues);
    UIColor *color3 = [[UIColor alloc]initWithRed:(pixel3.reds/255.0f) green:(pixel3.greens/255.0f) blue:(pixel3.blues/255.0f) alpha:1];

    Pixel *pix4 = [sortedPixels objectAtIndex:4];
    RGBAPixel *pixel4 = pix4.rgbpixel;
    NSLog(@"color4: %i %i %i:", pixel4.reds, pixel4.greens, pixel4.blues);
    UIColor *color4 = [[UIColor alloc]initWithRed:(pixel4.reds/255.0f) green:(pixel4.greens/255.0f) blue:(pixel4.blues/255.0f) alpha:1];

    Pixel *pix5 = [sortedPixels objectAtIndex:5];
    RGBAPixel *pixel5 = pix5.rgbpixel;
    NSLog(@"color5: %i %i %i:", pixel5.reds, pixel5.greens, pixel5.blues);
    UIColor *color5 = [[UIColor alloc]initWithRed:(pixel5.reds/255.0f) green:(pixel5.greens/255.0f) blue:(pixel5.blues/255.0f) alpha:1];

    colors = [[NSArray alloc] initWithObjects:color1,color2,color3,color4,color5, nil];

    myColors = colors;

    CFRelease(data);

}
</code></pre>

<p>}</p>

<p>My code doesn't really works! Thanks in advance!</p>
