2

Ultimately I'm working on a box blur function for use on iPhone.

That function would take a UIImage and draw transparent copies, first to the sides, then take that image and draw transparent copies above and below, returning a nicely blurred image.

Reading the Drawing with Quartz 2D Programming Guide, it recommends using CGLayers for this kind of operation.

The example code in the guide is a little dense for me to understand, so I would like someone to show me a very simple example of taking a UIImage and converting it to a CGLayer that I would then draw copies of and return as a UIImage.

It would be OK if values were hard-coded (for simplicity). This is just for me to wrap my head around, not for production code.

Cœur
  • 37,241
  • 25
  • 195
  • 267
willc2
  • 38,991
  • 25
  • 88
  • 99
  • @Joe - If you have something to answer, please do. The point of this site is not to gain arbitrary points, but to help others. The score is merely a reinforcer for positive behavior. Just because someone doesn't vote on your answer doesn't mean you wasted time in writing it. At the very least, you might have learned something in the process of answering the question. – Brad Larson Jan 05 '11 at 15:45
  • @Joe If you want to help, help. – willc2 Jan 08 '11 at 19:27

1 Answers1

2
UIImage *myImage = …;
CGLayerRef layer = CGLayerCreateWithContext(destinationContext, myImage.size, /*auxiliaryInfo*/ NULL);
if (layer) {
    CGContextRef layerContext = CGLayerGetContext(layer);
    CGContextDrawImage(layerContext, (CGRect){ CGPointZero, myImage.size }, myImage.CGImage);

    //Use CGContextDrawLayerAtPoint or CGContextDrawLayerInRect as many times as necessary. Whichever function you choose, be sure to pass destinationContext to it—you can't draw the layer into itself!

    CFRelease(layer);
}

That is technically my first ever iPhone code (I only program on the Mac), so beware. I have used CGLayer before, though, and as far as I know, Quartz is no different on the iPhone.

… and return as a UIImage.

I'm not sure how to do this part, having never worked with UIKit.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • Where does destinationContext come from? Do I just declare a new one like this: CGContextRef destinationContext; – willc2 Aug 18 '09 at 13:53
  • The destination context is the one you're going to draw the layer into. Assuming you're not creating the destination context yourself, you'd get it from the context stack by calling `UIGraphicsGetCurrentContext`. – Peter Hosey Aug 18 '09 at 14:59
  • Of course, if you plan to make a UIImage instead of draw in a view, you probably will make the context yourself. You'd create a bitmap context, draw into it, then use its buffer to create a CGImage with which to create a UIImage. (There may be an easier way to do this.) – Peter Hosey Aug 18 '09 at 16:03
  • Hello @PeterHosey, I have a image got from a UIBezeirPath, I want to draw it on CGLayer, I tried your code, so when I draw a line from bottom of the screen, it is drawn from top left to bottom right. Can you tell me why this might be happening – Ranjit Jan 28 '14 at 07:25
  • @Ranjit: You should ask a separate question about that. – Peter Hosey Jan 28 '14 at 07:43
  • @PeterHosey, why a different question, it is same right, you get a image from UIBezierPath or any, the method will be same right – Ranjit Jan 28 '14 at 07:56
  • @Ranjit: This question is about how to do it at all, starting from a blank slate. You're already doing it, but your output is wrong. That's a different question. – Peter Hosey Jan 28 '14 at 15:40
  • @PeterHosey, I figured it out. – Ranjit Jan 29 '14 at 05:14
  • Hello @PeterHosey, I have doubt and I have created a SO question for it, struggling with it, but not able to figure out, please help http://stackoverflow.com/questions/21438586/undo-redo-for-drawing-in-ios – Ranjit Jan 31 '14 at 07:01
  • Hello @PeterHosey, you just edited my question, any suggestions where I am going wrong – Ranjit Jan 31 '14 at 08:07
  • Hello @PeterHosey, I released the layer, but still instruments shows a leak, what can be the reason – Ranjit Mar 19 '14 at 06:52
  • @Ranjit: You should ask a separate question about that. – Peter Hosey Mar 19 '14 at 18:07