5

How to draw an image histogram of a UIImage on iPhone programmatically? e.g.:

alt text

Thanks!

Andriy
  • 2,767
  • 2
  • 21
  • 29
ohho
  • 50,879
  • 75
  • 256
  • 383

4 Answers4

4

You can calculate histogram with help of Accelerate framework. There is function vImageHistogramCalculation_ARGB8888(). To call this function you need to create vImage_Buffer from the UIImage. Use vImageBuffer_InitWithCGImage() function for it.

For more information you can add this framework to your project and read docs in the header files:

Accelerate.framework/Frameworks/vImage.framework/Headers/vImage_Utilities.h Accelerate.framework/Frameworks/vImage.framework/Headers/Histogram.h

kelin
  • 11,323
  • 6
  • 67
  • 104
2

There's no api for that.

Edit: An API was introduced with iOS5. See kelin's answer.

You would want to get the raw data of the image, count the color values and draw the histogram yourself.

Access the pixels: How to get pixel data from a UIImage (Cocoa Touch) or CGImage (Core Graphics)?

Use CoreGraphics to draw the histogram in a custom UIView subclass.

Community
  • 1
  • 1
tonklon
  • 6,777
  • 2
  • 30
  • 36
  • I disagree with your statement that there's no api. See my answer. – kelin Sep 25 '15 at 08:59
  • @kelin You are right. This API was introduced 1 year after my answer. So it is not really disagreeing, but updating. I have modified my answer and upvoted yours. – tonklon Nov 03 '15 at 10:08
1

Actually there is, I'll refer your to Brad Larson's answer : https://stackoverflow.com/a/10359305/300115

You can find the class GPUImageHistogramFilter in his library : https://github.com/BradLarson/GPUImage

The "Filter Showcase" demo in the lib already has a working version of a live histogram analysis. Live Histogram Analysis

Community
  • 1
  • 1
rnaud
  • 2,610
  • 32
  • 38
0

You might be interested in DSBarChart. Give it a try.

dhilipsiva
  • 3,688
  • 1
  • 24
  • 35
  • 2
    Rather than just link to your open source project, could you provide a more detailed example of how you'd use your project to create a histogram like the one above? That might better help people who are looking at your framework for this. – Brad Larson Dec 27 '12 at 21:03