0

I have a stupid question. I want to load a JPEG file and do some image processing. In the processing, the image file must be in pixel=by-pixel (unsigned char*), or in the format .bmp. I want to know how can I do this?

After processing, I want to save this file as .bmp or .jpg, how can I do it? Thanks very much and sorry for my poor English.

  • Take at look at this SO question on [How to get the RGB values for a pixel on an image on the iphone](http://stackoverflow.com/questions/144250/how-to-get-the-rgb-values-for-a-pixel-on-an-image-on-the-iphone) – alex Aug 16 '12 at 07:52
  • As alex points out, you can load your JPEG into a UIImage and follow the above answer. When done, you can create a UIImage from that and use one of the helper functions to make a JPEG or PNG from that. – Brad Larson Aug 16 '12 at 16:05

1 Answers1

0

The link int the comments is useful but not exactly what I think you want to do. You will have to read up a bit on Quartz, Apple's technology for drawing and image processing. Apple has great documentation on this, but its not a simple one hour type of effort - plan on at least a day maybe more.

Assume you start with a UIImage (I'm guessing, its not all critical):

  • get a CGImageRef to that image (possibly [UIImage CGImage])

  • create a CGBitMapContext that is large enough to render that image into (you can ask a CGImage for its width and height, etc).

  • if you want to ultimately create a PNG with alpha, you will need to create a bitmap context big enough for alpha, even if the JPEG image does not have it. If you want another JPEG your job is a bit easier.

  • render the image into the bit map context (there is a CGContextDraw... routine for that)

Now your image is contained in a bitmap that you can read and modify. The layout will probably be r-g-b unless you specified alpha, in which case you will have to determine where the alpha byte is.

Once you have the bit map modified, you can create a new CGImage from that bitmap, and with that you can get a UIImage if you want.

PS: you will find much sample code and posting about this if you search, so you will not be totally on your own.

David H
  • 40,852
  • 12
  • 92
  • 138